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.

bug: useMediaQuery doesn't clean up after itself

See original GitHub issue

Is 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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
CollierCZcommented, Mar 10, 2021

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!

1reaction
CollierCZcommented, Mar 10, 2021

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.

Read more comments on GitHub >

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

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