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.

NextJS & SSR Bug: Warning: Prop className did not match on useMediaQuery

See original GitHub issue

Description

On a NextJS application, when using Chakra’s useMediaQuery to determine a value for a Text prop, it prints an error about a non matching className on the server and the client

Link to Reproduction

https://codesandbox.io/s/festive-keller-6jhy5

Steps to reproduce

IN THE SANDBOX WORKS:

  1. Run the code and check the console

IN MY NEXTJS APP WITH CHAKRA DOES NOT. Am I missing something?

Code reference (same as codesandbox but in real app) same happening to me when ussing useMediaQuery as so:

const useDevice = () => {
  const [isLargeDevice] = useMediaQuery('(min-width: 62em)')

  return {
    isLargeDevice,
  }
}

then used: (THIS THROWS AN ERROR)

  import { useDevice } from './useDevice'
  
  const { isLargeDevice } = useDevice()
  
   <Text color='white' textStyle={isLargeDevice ? 'body.bold.lg' : 'body.bold.md'}>
       Don't miss out on new proposals
   </Text>

used: (THIS DOESN’T)

   <Text color='white'>
       Don't miss out on new proposals
   </Text>

screenshot here: image

Chakra UI Version

1.6.4

Browser

Google Chrome 91.0.4472.114

Operating System

  • macOS
  • Windows
  • Linux

Additional Information

Relates to this issue:

https://github.com/chakra-ui/chakra-ui/issues/684

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

20reactions
segunadebayocommented, Jul 12, 2021

Hi @niconiahi,

As mentioned in the docs,

Keep in mind this API relies on the user’s browser support of window.matchMedia and will always return false if it is not supported or does not exist (e.g. during server-side rendering).

To fix this, I recommend waiting for the client to be re-hydrated by using a useEffect check, then you can return the value from useBreakpointValue

const [mounted, setMounted] = useState(false)
const value = useMediaquery(...)

useEffect(()=>{ 
  setMounted(true)
},[])

return mounted ? value : null

Be sure to add a spinner or skeleton to that portion of the page so there’s no layout shift.

1reaction
aboveyunhaicommented, Feb 4, 2022

So there is a mismatch in the className between server and client, and the solution is to add a spinner and set the value with useEffect to fix a media query? Maybe it’s me, but that doesn’t sound right.

The idea of this code is to avoid rendering the hook value in server side because server does not have media query. didMount in useEffect is to enforce the hook value is only used in the client side. Nothing to do with loader. Spinner is just to make it look nicer and smoother in terms of UI/UX.

This particular hook is client side only. That’s why I asked to include the implementation in doc, instead of using one line of text to mention it doesn’t work in server side rendering. But depending on the readers’ background knowledge, this also might not immediately obvious for them. It requires you to know a little bit of SSR, CSR and some framework (like next.js) rendering process.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React + Material-UI - Warning: Prop className did not match
The problem is the SSR rendering in Next.js, which produces the style fragment before the page is rendered. Using Material UI and Next.js...
Read more >
[Next.js] Next.js에서 Prop `className` did not match 경고가 ...
바닐라 자바스크립트를 좋아하는 개발자입니다.
Read more >
Handling the React server hydration mismatch error
For prop errors, the warning looks something like: Warning: Prop `className` did not match. Server: "positive" Client: "zero".
Read more >
react-hydration-error - Next.js
'red' : 'blue' // As color is passed as a prop there is a mismatch between what was rendered server-side vs what was...
Read more >
Migrating to v5: getting started - Material UI - MUI
If you are using Next.js and you are not sure how to configure SSR to work with both ... Material UI v5 includes...
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