-
Notifications
You must be signed in to change notification settings - Fork 934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infinite render loop when the toggle button's ref is stored in a state variable #1540
Comments
Experiencing this issue as well. Originally, I noticed this as we use Downshift alongside Floating UI which uses this method to store their refs through a callback ref - I've replicated that behaviour in CodeSandbox as well. I believe this comment in the Formik issue is similar to what we're experiencing, with the stack trace pointing to the Also similar to the other issue reported with Formik, it seems like the |
Hi I've run into the same kind of the issue today, If you look closer into implementation it does make sense that re-render occurs, following props getters:
Are calling function Component() {
const combobox = useCombobox({ items: [] });
const [ref, setRef] = useState(null)
return <div {...combobox.getToggleButtonProps({ ref: setRef })} />
}
getToggleProps() {
....
refs.forEach(() => ref(node)) //call setRef (which is actually `setState` call) and re-renders if underlying dom node is different (different reference).
...
} One thing is weird to me though, why this is only reproducible on My fix for this issue is wrapping call const setReference = useCallback(
(node) => {
_setReference(node);
},
[_setReference]
);
const toggleProps = useMemo(() => {
return getToggleButtonProps({ ref: setReference });
}, [setReference]);
<div {...toggleProps} /> |
I tried to repro the issue on local, and I could not do it so far. I tried to copy all the relevant code from both @aliceHendicott and @nilscox sandboxes, and when I go to the responsive mode and pick an element, it works as expected. Can someone help me repro this on local? I am using the docusaurus based I'm thinking that having this repro on local will help with debugging. Thanks! |
Actually just use this branch. #1549 |
What does not make sense at all is why only the clicking items triggers this infinite loop. Toggling the button works, arrow keys work, enter key works. So far I can only tell that, after the clicking action happens, the dispatch kicks a ItemClick state change, state changes, render happens, and then the useControlledReducer will go wild. It will re-calculate state with the ItemClick state change, apparently the new state is different, another render happens, then the re-calculate happens again, and so on. |
I'm also experiencing this issue but only with Android devices! (I've only tried it on Chrome so far) |
Everyone, try |
Hey @silviuaavram, thanks for taking the time to look into it. I added a commit on a fork of your branch to move the I was able to test |
#1564, i will follow here. Thanks |
If someone wants to investigate why this issue happens, feel free. We should definitely try to fix it on our end, it might also have something to do with #1447. As a workaround, why not use |
Hi @silviuaavram, problem seems to be on getToggleButtonProps when i pass ref: setReference from floating-ui i have error: If the ref prop is not passed, everything works fine. |
@danichim why don't you use |
I use refs from useFloating, needs to be re-render when you scroll. On 9 Jan 2024, at 11:54, Silviu Alexandru Avram ***@***.***> wrote:
@danichim why don't you use useRef?
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Got it. I was simply curious. It's important that we fix this issue, but I don't have bandwidth at the moment for it. If someone can figure it out I will gladly review the changes. Thanks! |
@aliceHendicott Can you check this sandbox https://codesandbox.io/p/sandbox/react-v18-callback-ref-downshift-forked-ggr3wp it works without any change on library. |
https://codesandbox.io/p/sandbox/polished-smoke-tt737w A solution for your code @nilscox |
My use case is also to use downshift with floating-ui, here is a minimal repro with both libraries. Thanks for finding workarounds @danichim, your solution works if we also memoize the menu props (and exclude the I won't have much bandwidth myself these days, but I'll let you know if I do one day. |
Hey everyone! I think I got it. I also released the 8.3.2-alpha.0 version if you'd like to test it out. Please also check the details in the PR, see if they make sense, and let me know what you think! |
It might be a new issue, but with very similar outcome, I still see a complete crash of the app when using mobile Safari and selecting a value in the modal, reporting it just in case someone is also pulling their hairs out, I haven't received a useful stack trace yet. |
I'm a bit confused about versions currently - in my case, the |
downshift
version: 8.1.0node
version: 18pnpm
version: 8.6.12Relevant code or config
What you did:
I am trying to set a ref on the toggle button of a custom Select component.
What happened:
Selecting an item on a mobile device triggers a re-render loop.
Reproduction repository:
Open this codesandbox's app in a new tab, open the devtool with touch simulation enabled, click the label and select an item.
Problem description:
This only happens when trying to store the toggle button's ref in a state variable. The issue does not happen when removing the
ref
prop given togetToggleButtonProps
.This only happens with
react-dom
'screateRoot
function, so it may be related to #1384.This issue is similar to #1511, but it happens without any other library like formik or floating-ui and the stack trace isn't the same, so I assumed this is a different underlying issue.
The text was updated successfully, but these errors were encountered: