bug: useMediaQuery doesn't clean up after itself
See original GitHub issueIs your feature request related to a problem? Please describe.
useMediaQuery hook breaks SSR on nextjs, producing mismatch errors and unexpected rendering
Describe the solution you’d like i’m very beginner in react but with some search i found this answer here _Originally posted by @lfades in https://github.com/vercel/next.js/discussions/17443#discussioncomment-87097_
so i used react useEffect as a wrapper to update a local state value on useMediaQuery changes. so i can achieve the navigationbar example turns into drawer on mobile
const [queryLargeMobile, setqueryLargeMobile] = useState(true);
const { isLargeMobile } = useMediaQuery();
useEffect(() => {
setqueryLargeMobile(isLargeMobile);
return () => {};
}, [isLargeMobile]);
Describe alternatives you’ve considered i’m not sure if my solution is good but would be nice if useMediaQuery support SSR.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
useMediaQuery - doesn't select the corect state on initial load
Bug report When using the hook useMediaQuery(["(min-width: 980px)"]) for example - the state only updates after I manually come near the ...
Read more >how to use useMediaQuery in class component - Stack Overflow
So I did some research and found out you can use it in a class by doing this: import React from 'react'; import...
Read more >useMediaQuery - Chakra UI
useMediaQuery is a custom hook used to help detect whether a single media query or multiple media queries individually match.
Read more >Demystifying useEffect's clean-up function - Max Rozen
If you take nothing else away from this article, remember this: useEffect's clean-up function doesn't just run on unmount (assuming your dependency array ......
Read more >The 100% correct way to do CSS breakpoints - freeCodeCamp
Conclusion: communication is important. Don't purposefully detach yourself from helpful vocabulary. Tip #3: Be declarative. I know, I know, that ...
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 FreeTop 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
Top GitHub Comments
And I think we determined the source: https://github.com/kiwicom/orbit/blob/master/packages/orbit-components/src/ThemeProvider/QueryContext/useMediaQueryContext.js#L68 We were adding a listener instead of removing it. 🤦 Should be a fix out soon. Thanks again for your help!
I totally understand the difficulties of being a beginner and not knowing whether the error is coming from what you did or a specific library. I’m going to reopen the issue, because that new issue seems like a bug in the hook. Thanks for the report and the clear reproduction. We’ll be looking into it so I hope we’ll get a fix.