fix(Provider): throw a clearer error when rendered in a React Server Component (#2082)#2342
Open
veksa wants to merge 1 commit into
Open
fix(Provider): throw a clearer error when rendered in a React Server Component (#2082)#2342veksa wants to merge 1 commit into
Provider): throw a clearer error when rendered in a React Server Component (#2082)#2342veksa wants to merge 1 commit into
Conversation
commit: |
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.
Closes #2082.
If you render
<Provider>in a Server Component you currently get a pretty useless error. In React'sreact-serverbuild the effect hooks just don't exist (useEffect/useLayoutEffect/useRef/useStateare allundefined— onlyuseMemo,use,useIdand friends survive), souseIsomorphicLayoutEffectends upundefinedand you hituseIsomorphicLayoutEffect is not a function. Adding"use client"doesn't really help either — as noted in the issue,store/contextaren't serializable so you just trade it for a different confusing error.So this does what the thread landed on: an explicit dev-only check at the top of
<Provider>, before any hook runs, that throws a message actually telling you what's wrong and to add"use client".The
"react-server"export condition already handles the normal Next.js case, but only when the bundler applies it toreact-reduxtoo. This is the fallback for the "and many others" setups where React resolves to its server build but we don't — and since every app renders<Provider>, that's a good enough place to catch it.On testing (which came up in the thread): we don't need a real RSC environment. The whole thing hinges on
useIsomorphicLayoutEffectbeingundefined, so the test just mocks it toundefinedand checks<Provider>throws. Without the guard you'd get... is not a functioninstead, which doesn't match, so the test really is hitting the new branch.Kept it scoped to
<Provider>— the hooks would blow up in RSC too, but the Provider is the earliest thing everyone renders.Changes:
src/components/Provider.tsx— the guard (dev only, stripped in prod)test/components/Provider-rsc.spec.tsx— new test, separate file so the module mock doesn't leak into the other Provider tests