Skip to content

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

Closed
Kyzegs opened this issue Nov 1, 2022 · 10 comments
Closed

Ensure that useMediaQuery has the "correct" initial value #1000

Kyzegs opened this issue Nov 1, 2022 · 10 comments
Labels

Comments

@Kyzegs
Copy link

Kyzegs commented Nov 1, 2022

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.

@Kyzegs
Copy link
Author

Kyzegs commented Nov 1, 2022

I'll try to have an environment with the issue up later today.

@Kyzegs
Copy link
Author

Kyzegs commented Nov 1, 2022

Here's the environment with the issue.

@ArttuOll
Copy link
Contributor

I looked into this and I understand your problem. I'm currently trying to figure out a solution.

ArttuOll added a commit that referenced this issue Nov 20, 2022
…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
ArttuOll added a commit that referenced this issue Nov 21, 2022
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
@xobotyi xobotyi closed this as completed in 087b2b1 Dec 2, 2022
github-actions bot pushed a commit that referenced this issue Dec 2, 2022
# [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.
@xobotyi
Copy link
Contributor

xobotyi commented Dec 2, 2022

🎉 This issue has been resolved in version 20.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kylemh
Copy link
Contributor

kylemh commented Dec 6, 2022

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 useMediaQuery to include a second argument of { initializeWithValue: false } in order to work in an SRR-based environment.

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.

@xobotyi
Copy link
Contributor

xobotyi commented Dec 6, 2022

Your variant will cause hydration error, since SSR state and browser one will differ.

@kylemh
Copy link
Contributor

kylemh commented Dec 6, 2022

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.

@xobotyi
Copy link
Contributor

xobotyi commented Dec 6, 2022

Overall - i've missed it during review, since all other hook causes hydration error on missconfig rather than SSR crash.

@ArttuOll please, add isBrowser guard to the code so SSR at least works.

@ArttuOll
Copy link
Contributor

ArttuOll commented Dec 7, 2022

No problem. I'll take a look later today.

@ArttuOll
Copy link
Contributor

ArttuOll commented Dec 7, 2022

@kylemh Just wanted to let you know that we're working on this feature #1023 , which would improve DX for SSR significantly. It's still a WIP though - all help appreciated.

ArttuOll added a commit that referenced this issue Dec 7, 2022
…d of SSR crash

In #1000 it was noted, that the current implementation does not follow the convention of causing a
hydration error on misconfiguration. This is now fixed.

re #1000
ArttuOll added a commit that referenced this issue Dec 7, 2022
…d of SSR crash (#1042)

In #1000 it was noted, that the current implementation does not follow the convention of causing a
hydration error on misconfiguration. This is now fixed.

re #1000
github-actions bot pushed a commit that referenced this issue Dec 7, 2022
## [20.0.1](v20.0.0...v20.0.1) (2022-12-07)

### Bug Fixes

* **useMediaQuery:** On misconfiguration, cause hydration error instead of SSR crash ([#1042](#1042)) ([46e5bcc](46e5bcc)), closes [#1000](#1000) [#1000](#1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants