[useMediaQuery] defaultMatch is false before hydratationCompleted
See original GitHub issueHey,
I just ran into an race condition with the useMediaQuery hook. If you use this in client side rendering, the first return is false. The component will rerender immediately due to the useEffect and will show the correct value.
I think, this is a bad behavior, because a useMediaQuery will most likely change the component, which causes a lot of unnecessary rerenders.
- This is not a v0.x issue.
- I have searched the issues of this repository and believe that this is not a duplicate.
Expected Behavior 🤔
It should render once and return the correct value.
Current Behavior 😯
It renders twice and returns the wrong value in the first place.
Steps to Reproduce 🕹
Link: https://stackblitz.com/edit/react-fcrunc
- Load the site and open the console. Site shows the actual state. Console shows all history states.
Context 🔦
I want to create a defaultValue for a hook to decide, if I want to show a Sidebar or not.
My default value is false
the entire time due to this bug.
Your Environment 🌎
Tech | Version |
---|---|
Material-UI | v3.9.1 |
React | 16.7.0-alpha2 |
Browser | Chrome 71 and Firefox 64 |
TypeScript | 3.2.4 |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:21 (16 by maintainers)
Top Results From Across the Web
[useMediaQuery] defaultMatch is false before ... - GitHub
Hey, I just ran into an race condition with the useMediaQuery hook. If you use this in client side rendering, the first return...
Read more >React 18: Hydration failed because the initial UI does not ...
I have react 18.2.0 with next 12.2.4 and I fix hydration with this ... @emotion/react version 10 and above works with NextJS by...
Read more >Avoiding hydration mismatch when using React hooks
Dependencies in custom React hooks; Avoiding hydration mismatch when using React ... Initialize 'matches' with 'false' on the server, and with the correct....
Read more >Handling the React server hydration mismatch error
It's when the useEffect() Hook executes that the real value is set. This is the 2-pass solution.
Read more >`useMediaQuery`hook that actually works with SSR : r/nextjs
I've tried react-responsive , Material UI's useMediaQuery hook, ... but I get a fast SSR'd page that renderers correctly before hydration.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
There’s a nasty case with this unfrequent option. I needed to set drawer initial state open on desktop, and hidden on mobile. I did this:
The issue with
useMediaQuery
jumping from one state to another hasn’t bitten me before. But here it doesn’t work, the initial state is set, and I don’t see a good alternative to correct it when matchesDown600 is updated.I also tried but didn’t find a good way to go with a class component (you could use the componentDidMount). There you can’t have this hook. This component is the layout, so no props passed from above.
Correcting to this works:
I just found my problem. I need to set
noSsr
in the options object as I’m not doing server-side rendering. This is not very intuitive and will lead to many bugs. We may want to change this behavior. ~noSsr
is also missing on the typing for the options object.~The documentation: https://next.material-ui.com/components/use-media-query/#client-side-only-rendering