-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Hydration mismatch after SSR using useMediaQuery #1230
Comments
I do not understand how providing a default value would solve the mismatch, what if the default value will not match the value on client? |
The default value would work best if it defines the initial state of the client and the server. After the initial client hydration, it's ok for these to not mach in the following states after the result of the media query is resolved. |
I still do not get the issue, currently I think that the logic is:
As far as I know, there is not any middle step where we can insert any additional logic. |
Currently there are only the two states that you mentioned.
React can't correctly hydrate the html generated on the server using fase because the client's virtual dom was generated using the result of the media query: true. Adding a default value could solve this issue by allowing the first render of the client to match the server for hydration. From there the use effect can set it to the result of the media query. |
Okay, I got the idea – if default value is provided, it must set value in |
Yes I believe so. It should be ok to set the initial state using the default value, but the use effect should re set this to the result of the media query after the first render. The result of the incorrect hydration from what I've seen is broken layouts, but only when the media query initial match is true, so It also may be helpful to throw or warn if typeof window is undefined and no default value is provided. |
What package has an issue
@mantine/hooks
Describe the bug
Hello, I have had an incredible time using Mantine and really appreciate all the incredible work the team has done putting this together for the community!
I came across this issue using useMedaQuery from @mantine/hooks that I was familiar with from another package. I wanted to point this out here in case support for SSR is on the roadmap. Here is repro using nextjs
index.tsx
The SSR of useMediaQuery has an initial state of false where the client defaults to the result of the media query: true
In which browser did the problem occur
chrome
If possible, please include a link to a codesandbox with the reproduced problem
No response
Do you know how to fix the issue
Yes
Are you willing to participate in fixing this issue and create a pull request with the fix
No response
Possible fix
Here is some more context around a fix for this. streamich/react-use#2216
The text was updated successfully, but these errors were encountered: