fix(react): keep PathnameProvider mounted so changing userId doesn't change hook count (#384)#704
Open
crossi-dev wants to merge 1 commit into
Open
Conversation
Previously FlowsProvider returned props.children directly (outside PathnameProvider) when userId was null. When userId later changed to a string, PathnameProvider was inserted into the tree for the first time, causing React to throw "Rendered more hooks than during the previous render" because the component subtree structure changed under the same parent fiber. Fix: always render PathnameProvider so it is stable in the tree; conditionally render FlowsProviderInner vs raw children inside it. Closes RBND-studio#384
Member
|
Hi @crossi-dev and thanks for the PR! I do like the idea, keeping the React component tree the same with or without the userId would prevent the app from rerendering completely after SSR/SSG hydration. However this PR seems incomplete to me. The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #384
Problem
FlowsProviderearly-returns whenuserIdisn't set yet:So while
userIdisnullthe children render outsidePathnameProvider. WhenuserIdthen changes to a string,PathnameProvider(which hasuseState/useEffect) is mounted into the tree at the same parent fiber for the first time — the render now runs more hooks than the previous one, and React throws "Rendered more hooks than during the previous render."Fix
Keep
PathnameProvidermounted at a stable position on every render, and switch the inner content instead:PathnameProvider's hooks now run on every render regardless ofuserId, so the hook count never changes across thenull → stringtransition.Verified
I added 3 regression tests covering mount-with-userId, mount-without then set userId, and the reverse. The
workspaces/reactpackage had no component-test harness yet, so the PR also addsreact-dom+@types/react-dom(devDeps) and"types": ["jest"]to its tsconfig — the minimal wiring needed to render the provider in a test. Happy to drop the test + that wiring and keep just the 4-line fix if you'd prefer a leaner diff.Came across flows-sdk and noticed #384 had been open a while, so I fixed it. No strings attached — I'm with Choreless and we like contributing real fixes to projects we use. If it's useful and you ever want a hand with something larger, happy to help.
— Charles