headlessui icon indicating copy to clipboard operation
headlessui copied to clipboard

Error when using menu and listbox with child function and fragment in React 19

Open isoroka-plana opened this issue 11 months ago • 3 comments

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

For example: v2.2.0

What browser are you using?

For example: Chrome

Reproduction URL

https://codesandbox.io/p/devbox/funny-wood-jtchxm

Describe your issue

After updating to React 19 following error has begun to appear in my console:

 Invalid prop `data-headlessui-state` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props. 

When direct child of Menu or Listbox elements is a react fragment (my use case is that I am using a function a function for children to access the component's APIs). Example:

<Menu>
  ({open}) => (
    <>
      <MenuButton>Text</MenuButton>
      {open && <MenuItems></MenuItems}
    </>
  )
</Menu>

There may be other components affected, I did not test.

isoroka-plana avatar Dec 16 '24 18:12 isoroka-plana

I am having the same issues.

Shawns2759 avatar Jan 09 '25 18:01 Shawns2759

Same here.

s-lukashenka-micoworks avatar Jan 16 '25 02:01 s-lukashenka-micoworks

I fixed this by replacing all the <> (Fragment) </> examples with <div> and </div> in my use of Headless UI Listbox:

<Menu>
  ({open}) => (
    <div>  {/* was <> */}
      <MenuButton>Text</MenuButton>
      {open && <MenuItems></MenuItems}
    </div> {/* was </> */}
  )
</Menu>

DoctorDerek avatar Apr 08 '25 17:04 DoctorDerek

Using as="div" on the top-level component (e.g. Menu, Listbox, etc.) also seems to achieve the same effect.

But this definitely seems like an oversight/bug. The components need to have multiple children, e.g. ListboxButton and ListboxOptions, and need to be able to support render props, and the only way to do that is with a Fragment.

vincerubinetti avatar Sep 09 '25 17:09 vincerubinetti

This should be fixed by #3788, and will be available in the next release.

You can already try it using:

  • npm install @headlessui/react@insiders.

RobinMalfait avatar Sep 10 '25 10:09 RobinMalfait