headlessui icon indicating copy to clipboard operation
headlessui copied to clipboard

`<Listbox>` error: `Uncaught Error: Maximum update depth exceeded.`

Open breadadams opened this issue 1 year ago • 11 comments

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v2.2.0

What browser are you using?

Chrome 129.0.6668.59

Reproduction URL

  • https://codesandbox.io/p/devbox/headlessui-listbox-uncaught-error-forked-jgqn4t
  • https://jgqn4t-5173.csb.app/

Describe your issue

We've noticed the following error on the production version of our app: https://react.dev/errors/185?invariant=185

image

Local error message:

image

To reproduce it (on macOS) you need to be using a mouse that isn't a Magic Trackpad/Mouse, or set the "Show scroll bars" option in Settings to "Always":

image

Steps to reproduce:

  1. Open the listbox and select an option (e.g. the first one).
  2. The application should crash.

Worth mentioning that you can prevent the error from occurring by either of the following options:

  • Removing the overflow-auto from the element on L6.
  • Commenting out the label on L8.

breadadams avatar Nov 18 '24 23:11 breadadams

FYI I've made some minor changes to the codesandbox and repro steps to make it simpler, and ensure that the error always occurs.

breadadams avatar Nov 19 '24 11:11 breadadams

We have the same issue, but it's so random I can't give a way to reproduce it.

Capture d’écran 2024-11-21 à 22 59 39

RobinBressan avatar Nov 21 '24 22:11 RobinBressan

I wasted three hours on this and in the end it doesn't look like I screwed anything up, so you can probably replicate the ListBox functionality with a Menu and Transition and you won't have this problem.

LianZiZhou avatar Nov 30 '24 09:11 LianZiZhou

I wasted three hours on this and in the end it doesn't look like I screwed anything up, so you can probably replicate the ListBox functionality with a Menu and Transition and you won't have this problem.

@RobinMalfait @reinink

LianZiZhou avatar Nov 30 '24 09:11 LianZiZhou

We have the same issue, it looks like the problem starts when the listbox options overflow the window, using portal (<Listbox.Options anchor="bottom" />) seems to resolve the problem for us.

ebeigarts avatar Dec 04 '24 11:12 ebeigarts

We also have the same issue. In our case, we notice it only happens on Windows device and remove transition prop from ListboxOptions will fix it.

TrustyTechSG avatar Dec 13 '24 05:12 TrustyTechSG

Same issue here. Using v2.2.0. This error is happening on a Listbox with about 200 options, but not on ones with much smaller lists. The only "solution" I've found was to remove the Transition wrapping the ListboxOptions.

LucasNovaes2016 avatar Jan 09 '25 18:01 LucasNovaes2016

In my case, the "Maximum depth exceeded" error happens once a selection is made on any option on a Listbox with 152 options. Only by removing the transition prop or setting its value to false, does the issue go away.

Also using v2.2.0 of the react package.

simply-alliv avatar Jan 21 '25 05:01 simply-alliv

I'm observing the same issue with v2.1.9 so its not related directly to v2.2.0. I believe this comes down: https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-react/src/components/listbox/listbox.tsx#L1041 where setLocalOptionsElement is supplied to the useSyncRefs function which sets the state directly in the render function.

This was introduced in this commit: https://github.com/tailwindlabs/headlessui/commit/071aa0e260603997f21894c7781b6e7f6f21d996#diff-cb335fbe544f778130fb8f7c8eeb065f05c0038ade35c572cbfee6c35dac05a5

@RobinMalfait

KingSora avatar Jan 27 '25 10:01 KingSora

I am using v2.2.0 as well. Adding static to <ListboxOptions /> seems to prevent the issue

adrianxadamn avatar May 09 '25 22:05 adrianxadamn

We have the same issue, it looks like the problem starts when the listbox options overflow the window, using portal (<Listbox.Options anchor="bottom" />) seems to resolve the problem for us.

I can confirm I encountered this issue today with both versions 2.2.2 and 2.2.3. It only occurs when ListboxOptions overflows the window and the transitions prop is enabled or the component is wrapped in a Transition.

josh-respectx avatar May 13 '25 04:05 josh-respectx

Hey!

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

You can already try it using:

  • npm install @headlessui/react@insiders.

RobinMalfait avatar Sep 03 '25 13:09 RobinMalfait

This is fantastic work. Thank you @RobinMalfait. Looking forward to testing this solution. At the moment, we've been handling this issue by setting the transition prop to be false by default. While this ensures the uncaught error of maximum depth exceeded does not occur, enabling transitions on all ListBox components would create a more fluid UI/UX for our users.

simply-alliv avatar Sep 03 '25 16:09 simply-alliv