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 does not work with SSR styles

See original GitHub issue

I 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: image

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:

image

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

github_iconTop GitHub Comments

4reactions
segunadebayocommented, Jan 3, 2021

Yes, useMediaQuery is a client-side only hook since it uses window.matchMedia. For this, I’d recommend using a media query style object in sx or css 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.

2reactions
marco-emmanuel-notocommented, Oct 26, 2021

I found this solution. #3124 (comment)

Also, I heard that useBreakpointValue supports SSR, so I tried it and it worked. Maybe like this.

  const isMinWidthLg = useBreakpointValue({ lg: true })

  return <GridItem mx={isMinWidthLg ? 1 : 0}>
  // or
  return <GridItem {...(isMinWidthLg && { mx: 1 })}>

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.

Read more comments on GitHub >

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

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