-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Best strategy for checking whether (typeof window !== undefined) while using Hooks #26448
Comments
Yes, as I showed in that snippet, but if it only matters for your hook as a condition, you don't need to wrap it like my example snippet shows there. That was for third-party imported components that were causing problems because they internally use
SSR will happen with a minimal viewport afaik, so expect anything that might bake in inline CSS or markup/components based on width to favor mobile breakpoints. You can also use the For the lack of updating/responsiveness in the situation you describe, it can be due to hydration. React hydrates from the SSR html it first loads, and assumes the state it would compute would match the HTML it received, and doesn't bother to check for a mismatch, so you need to trigger a re-render that would alter the returned JSX if I recall.. I have a basic PR here for If you instead get a momentary flicker, that'd be from the SSR output before React/JS kicks in, and may require additional handling, I have a PR showing that fix off too here. |
Thanks for taking the time to write all this, its very helpful. I have changed my code to not use the hook and use Do you have a sense of when they might approve the PR? It looks like all checks are fine. |
The flicker is probably the original HTML/CSS rendered from SSR, prior to React taking over. If it persists until changing state in React such as navigation or window resize event, then it may also be a hydration issue with the component.
Whenever they decide to review and approve my PRs, some are partly almost there but waiting on some feedback, slow process unfortunately.
Presumably, that's conditionally changing the rendered component based on the breakpoint chosen. So during SSR that would be fixed with the smallest breakpoint I think, whereas you may want something more dynamic. If it's working fine once the page is fully loaded and React has done it's thing, then it's fixable with CSS media queries. You can add those with It's not likely compatible with how that plugin is working, as if the markup is changing, not just CSS, you'll probably want to render all variants instead and use media queries to toggle visibility.
Another alternative is to hide the component at SSR(when window isn't available, like shown in the snippet of mine you quoted), it should minimize the issue, or you can use some CSS transition to fade-in/reveal the component. |
I "fixed" (went around) my problem by reading and following the suggestions in this great article: https://joshwcomeau.com/react/the-perils-of-rehydration/ (wrapping everything in a |
That's a bit odd, since that's a hydration issue not an SSR one where |
I am using a custom hook to access breakpoint information, which is using the
window
property, so I'm running into trouble when building.I am calling the hook from a component like so:
const breakpoint = useBreakpoints(typeof window !== undefined)
and here is the code I have for the hook itself:
The reason I am passing the
(typeof window !== undefined)
value from the components to the hook is because of the "Rules of Hooks": "Don’t call Hooks inside loops, conditions, or nested functions".This works, but results in a very weird behavior on deployed version where the page (on desktop mode) loads to the mobile layout and needs a couple of refreshes to correct itself.
Having done some reading in similar issues and answers I now understand a better way to go about it is to do something like this:
but I want to understand exactly what is happening that triggers this weird behavior on deploy?
The text was updated successfully, but these errors were encountered: