NextJS & SSR Bug: Warning: Prop className did not match on useMediaQuery
See original GitHub issueDescription
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:
- 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:
Chakra UI Version
1.6.4
Browser
Google Chrome 91.0.4472.114
Operating System
- macOS
- Windows
- Linux
Additional Information
Relates to this issue:
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top 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 >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 >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
Hi @niconiahi,
As mentioned in the docs,
To fix this, I recommend waiting for the client to be re-hydrated by using a
useEffect
check, then you can return the value fromuseBreakpointValue
Be sure to add a spinner or skeleton to that portion of the page so there’s no layout shift.
The idea of this code is to avoid rendering the hook
value
in server side because server does not have media query.didMount
inuseEffect
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.