hydration error in production but not in dev environment #36232
Replies: 13 comments 3 replies
-
Hi there, The React Hydration Error occurs using a specific library or application code that is relying on something that could differ between pre-rendering and the browser. An example of this is using An example: function MyComponent() {
// This condition depends on `window`. During the first render of the browser the `color` variable will be different
const color = typeof window !== 'undefined' ? 'red' : 'blue'
// As color is passed as a prop there is a mismatch between what was rendered server-side vs what was rendered in the first render
return <h1 className={`title ${color}`}>Hello World!</h1>
} How to fix it: // In order to prevent the first render from being different you can use `useEffect` which is only executed in the browser and is executed during hydration
import { useEffect, useState } from 'react'
function MyComponent() {
// The default value is 'blue', it will be used during pre-rendering and the first render in the browser (hydration)
const [color, setColor] = useState('blue')
// During hydration `useEffect` is called. `window` is available in `useEffect`. In this case because we know we're in the browser checking for window is not needed. If you need to read something from window that is fine.
// By calling `setColor` in `useEffect` a render is triggered after hydrating, this causes the "browser specific" value to be available. In this case 'red'.
useEffect(() => setColor('red'), [])
// As color is a state passed as a prop there is no mismatch between what was rendered server-side vs what was rendered in the first render. After useEffect runs the color is set to 'red'
return <h1 className={`title ${color}`}>Hello World!</h1>
} I hope this helps! |
Beta Was this translation helpful? Give feedback.
-
In my case, I had a page component, this component calls the components of another children components. This error happened only with gatsby build You can replicate error on localhost, making:
Details error: The children component have some problems with rect ^18.1.0 I am using: this error also causes the page to render 2 times (it's crazy) Please check if you have a child component that is causing this error. |
Beta Was this translation helpful? Give feedback.
-
I found another code mistake that causes minified error #418 in react I had minified react error #418 in my GatsbyJs website The problem was that I had some components lazy loaded but I forgot to wrap them in a Suspense tag // will cause the error
const Component= lazy(() => import('../../atoms/component'))
{...}
return <Component/>
// ok
const Component= lazy(() => import('../../atoms/component'))
{...}
return <Suspense fallback={null}>
<Component/>
</Suspense> Even in this case, the problem is that the rendered version of the site is different from the server one I hope this will help someone |
Beta Was this translation helpful? Give feedback.
-
Any progress on this? |
Beta Was this translation helpful? Give feedback.
-
I'm getting this error only when I add a
|
Beta Was this translation helpful? Give feedback.
-
I got tired of waiting, and switched to Astro. Chao |
Beta Was this translation helpful? Give feedback.
-
If its any consolidation, to someone, anyone... next.js has the same issue when building out their static sites: vercel/next.js#43921 |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
Is this anything to do with images being loaded from cache? I'm seeing a lot of these issues, especially when using images. I'm just wondering whether the javascript for images is running before the react javascript runs, replaces some of the dom and causes these issues. Not fully investigated, but when is made my images lazy, it seemed to fix some of these problems. |
Beta Was this translation helpful? Give feedback.
-
Suddenly getting this issue myself now, cant even figure out what could be causing it library wise |
Beta Was this translation helpful? Give feedback.
-
Using this caused the error: |
Beta Was this translation helpful? Give feedback.
-
I am having this error, because I used lazy loading. I didn't forget to wrap it in the React.Suspence component though. I will try to use export.replaceHydrateFunction though, maybe that will fix the issue. |
Beta Was this translation helpful? Give feedback.
-
Using It will resolve the error but it's effectively disabling hydration and just re-rendering everything client side so your time-to-interactive is still impacted and you are missing out on one of the core benefits of using Gatsby. Really this is just doing what React is doing for you when it encounters an error in hydration i.e. re-rendering everything and ignoring the static HTML already in DOM. The right solution is to find the cause of the discrepancy as described in the docs though this isn't easy at all. Another option is to use partial hydration and flag components that will only work on client side with 'use client' directive at top of file as described here |
Beta Was this translation helpful? Give feedback.
-
I keep getting this error in production
the page is linked to this message
this only happens in production and not development environment.
After that I added this flag and I can see the error in the development
this is happening because i am trying to show a loading message while fetching data, any ideas how to fix this?
I am using gatsby 4.19.2 and react 18.2.0
Beta Was this translation helpful? Give feedback.
All reactions