-
Notifications
You must be signed in to change notification settings - Fork 101
Ensure that useMediaQuery
has the "correct" initial value
#1000
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
Comments
I'll try to have an environment with the issue up later today. |
Here's the environment with the issue. |
I looked into this and I understand your problem. I'm currently trying to figure out a solution. |
…rst render instead of undefined This gets rid of the value changing from undefined to boolean on the first renders, which might trigger undesired side-effects for users. BREAKING CHANGE: On first render, boolean is returned instead of undefined. fix #1000
This allows us to give the users the media query match state immediately on first render and optionally give them SSR compatibility. BREAKING CHANGE: Returned value is no longer undefined on first render. Set initializeWithValue to false to re-enable this behavior. fix #1000
# [20.0.0](v19.2.0...v20.0.0) (2022-12-02) ### Bug Fixes * **useMediaQuery:** add option to martch media query on first render ([#1020](#1020)) ([087b2b1](087b2b1)), closes [#1000](#1000) ### BREAKING CHANGES * **useMediaQuery:** `useMediaQuery` and `useScreenOrientation` now returns matched media query state on first render by default, SSR users can change that behaviour via hook options.
🎉 This issue has been resolved in version 20.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
I wanted to suggest reverting this and/or thinking about it more. I've just upgraded this package to the latest version and have to replace all my usages of One suggestion might be to adjust this line: if (options?.initializeWithValue ?? true) to be: if (typeof window !== 'undefined' && (options?.initializeWithValue ?? true)) I think this still appeases the original request without ruining the DX for people that use this hook with Next.js or Remix. |
Your variant will cause hydration error, since SSR state and browser one will differ. |
I see. I didn't think about strict mode... I'm not sure what the workaround is, but it really sucked to be punished by this change. |
Overall - i've missed it during review, since all other hook causes hydration error on missconfig rather than SSR crash. @ArttuOll please, add |
No problem. I'll take a look later today. |
New Features
What is the new or updated feature that you are suggesting?
Ensure that
useMediaQuery
returns the matched value for the given query in the first render.Why should this feature be included?
In most cases, we do not want to load our animations on the initial page load. When we have a component with animations that relies on the
useMediaQuery
hook the following happens: The animation gets triggered because of the value from the hook changes. I've tried this with my animation implementation and libraries such as Framer Motion.Having the correct initial value would fix this problem.
The text was updated successfully, but these errors were encountered: