useMediaQuery - doesn't select the corect state on initial load
See original GitHub issue🐛 Bug report
When using the hook useMediaQuery(["(min-width: 980px)"])
for example - the state only updates after I manually come near the threashold set, but if I load my component under that width it will not trigger and update the component.
💥 Steps to reproduce
- Go to you component with
useMediaQuery([])
hook - Load the component under the threadshold set in the hook
- Notice that even though the state is correct the component doesn’t re-render
- Now - resize your screen
- Notice that the component triggers its re-render
🧐 Expected behavior
When using the useMediaQuery hook - when the component is initially loaded - trigger a re-render if under / above the threshold.
🧭 Possible Solution
Checking on load - if
🌍 System information
Software | Version(s) |
---|---|
Chakra UI | Latest |
Browser | Chrome Latest |
Operating System | OXS |
📝 Additional information
const CardSection: React.FC = () => {
const [isLargerThanMd, isDisplayingInBrowser] = useMediaQuery([
"(min-width: 980px)",
"(display-mode: browser)",
])
const CardComponents = [
<Card key={0} title="Cryptocurrencies" link="/dashboard" />,
<Card key={1} title="Stocks" link="/dashboard" />
]
function determinDevice() {
let component;
switch (isLargerThanMd) {
case false:
component = <VStack> {CardComponents} </VStack>
break
default:
component = <HStack> {CardComponents} </HStack>
break
}
return component
}
return (determinDevice())
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:13 (2 by maintainers)
Top Results From Across the Web
Setting state using React hooks does not work correctly
1 Answer 1 ... This is because the useMediaQuery hook itself has to compute your media query and then give you the result...
Read more >When to use useCallback, useMemo and useEffect
The useCallback, useMemo, and useEffect are a way to optimize the performance of React-based applications between rerendering of components.
Read more >Using media queries - CSS: Cascading Style Sheets | MDN
Media queries allow you to apply CSS styles depending on a device's general type (such as print vs. screen) or other characteristics such...
Read more >Using Breakpoints and Media Queries in Material-UI
Material-UI is one of the most popular React UI component libraries. ... To use the useMediaQuery hook, first import it from Material-UI.
Read more >CSS Media Min-Width & Max-Width Queries - How They Work
Media queries can be used to target certain resolutions or even specific email ... You can make 50% width fit if you set...
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
Current workaround with useEffect seems to fix useMediaQuery, but not the warning. I guess the issue is related to SSR in my case, since I have read about the warning in nextjs and SSR related posts elsewhere (https://github.com/chakra-ui/chakra-ui/discussions/2920 & https://lihautan.com/hydrating-text-content/ )
I can confirm this happens on SSR (14810). I am using nextjs and the issue is that the server is sending the rendered page for desktop, but the client is not getting re-rendered when this useMediaQuery() hook executed.
This happens only on the initial load. I solved it using this hook as suggested in nextjs repo, modified it a bit to choose useLayoutEffect only on client.
Also, for responsive styles you need to add meta tag --> meta tag for viewport
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
For my case it worked without even adding the above meta tag.