SSR: rehydration broken for React.Context #12579
-
On initial renders, rehydration doesn't seem propagate React.Context correctly down the component chain. To ReproduceHere's a minimal repro: https://github.com/AndrewSB/reduced-context-bug Expected behaviorThe page should show "Compact" Actual behaviorThe page shows "Normal" ScreenshotsSee video in README of linked repo System information
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
This probably has to do with how https://github.com/streamich/react-use/blob/1c7a000735ef05c7391de8078cdfe9ca87d877ec/src/util.ts#L1 |
Beta Was this translation helpful? Give feedback.
-
that makes sense. What might be a better way to handle this kind of server/client detection? |
Beta Was this translation helpful? Give feedback.
-
I think you have to not treat (If you To eliminate that disconnect between server-render and client-dom, you can do one of two things:
|
Beta Was this translation helpful? Give feedback.
I think you have to not treat
Infinity
(which is what react-use'suseWindow
hook returns if it's called as part of SSR) as if it has meaning for your render. Doing so sets you up for a disconnect between the server-rendered HTML (which rendered as if the page were infinitely wide) and the client-rendered HTML (which renders with the proper page width), which is actually what is causing your error.(If you
console.log(displayContext);
in yourChild
component, you can see that the react context is, in fact, passing the right value to the component. The error is that component generates an error on the client-side when it tries to attach to the server-rendered DOM.)To eliminate that disconn…