tailwindlabs / headlessui

Completely unstyled, fully accessible UI components, designed to integrate beautifully with Tailwind CSS.
https://headlessui.com
MIT License
25.02k stars 1.03k forks source link

React warning `flushSync was called from inside a lifecycle method` when conditionally displaying an immediate and autofocused combobox #3334

Open augustl opened 5 days ago

augustl commented 5 days ago

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

2.1.1

What browser are you using?

Firefox

Reproduction URL

https://codesandbox.io/p/devbox/competent-lichterman-wxf42g

Describe your issue

The reproduction above sets up the following scenario:

When changing the state to setShowCombobox(true), React yields the following warning:

Warning: flushSync was called from inside a lifecycle method. React cannot flush when React is already rendering. Consider moving this call to a scheduler task or micro task.

The code in headlessui that seems to cause this: https://github.com/tailwindlabs/headlessui/blob/abd86fca3e3dac3bb9c884ecc2012a590e33d29a/packages/%40headlessui-react/src/components/combobox/combobox.tsx#L1323

The specific scenario in my app is a data grid. The grid starts out in "read mode", and displays values as plain divs with text. When cells in the grid are focused, they enter "edit mode", and my headlessui combobox based editor appears conditionally and is immediately focused, causing the flushSync in headlessui to be called while React renders the data grid cell.