useMediaQuery does not work with SSR styles
See original GitHub issueI have tried to create a codepen for it, but was unable to: https://codesandbox.io/s/no-margin-chakra-forked-zcygd?file=/src/App.tsx
The issue seems to be that everything is reported correctly, the only issue being the styles are out of alignment with what is being rendered and reported:
const [baseLg] = useMediaQuery((max-width: 35em))
const marginX = `${space[baseLg ? 1 : 0]} !important`
console.info(marginX)
return (
<GridItem
data-marginX={marginX}
mx={marginX}
>
)
Output:
So on initial load, the text rendered is correct, but the styles are incorrect. Then when i change from portrait to landscape back to portrait, the styles are correct. As seen here:
I am unsure, but I assumed that useMediaQuery would generate css media queries, rather than imitate media queries with JS.
It may not be an issue, but it feels like one to me 😃
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
`useMediaQuery`hook that actually works with SSR : r/nextjs
I was really fixated on the idea of applying media queries while using inline styles because it's how I'm already styling my components....
Read more >How to implement SSR for Material UI's media queries in ...
First a caveat -- I do not currently have any experience using SSR ... from "@material-ui/core/styles"; const mobileSsrMatchMedia = query ...
Read more >Media queries in React for responsive design - Material UI - MUI
useMediaQuery. This is a CSS media query hook for React. It listens for matches to a CSS media query. It allows the rendering...
Read more >useMediaQuery - Chakra UI
The useMediaQuery hook accepts a single media query or an array of media queries, and optionally an object that contains an ssr boolean...
Read more >Combining Server-Side Rendering and Responsive Design
Let's explain the challenge that comes up when mixing SSR and responsive ... Note: We're going to use React mainly to describe the...
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
Yes, useMediaQuery is a client-side only hook since it uses
window.matchMedia
. For this, I’d recommend using a media query style object insx
orcss
prop, like @callumflack suggested.You can also consider tweaking the breakpoint in the theme as well and passing responsive values (as object or array).
Cheers.
True but always be careful because this approach for styling can lead to a huge CLS when doing server-side rendering, which in turn will hinder your site’s performance.