question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[useMediaQuery] defaultMatch is false before hydratationCompleted

See original GitHub issue

Hey,

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

  1. 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:21 (16 by maintainers)

github_iconTop GitHub Comments

35reactions
artegencommented, Jul 15, 2021

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:

 const matchesDown600 = useMediaQuery('(max-width:600px)');
  
 const initialOpen = matchesDown600 ? false : true;
 const [open, setOpen] = React.useState(initialOpen);

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:

 const matchesDown600 = useMediaQuery('(max-width:600px)', { noSsr: true });
21reactions
johannwagnercommented, Jul 15, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found