This is a common error when using React with Server Side Rendering (SSR) or any another type of pre-generated HTML.
Fortunately, the fix is fairly simple, and I also published an NPM package that can help make the solution even easier.
As the error suggests, this problem occurs when the HTML from the server does not match what the client-side is trying to render.
This can happen any time you have dynamic text. For example, if you are rendering dates or times, different environment may return different results:
hydration failed because the initial ui does not match what was rendered on the server.?
This code is dependent on the current time. Since the pre-rendered HTML and the client-side app are rendering at different times, the time returned by new Date()
will be different.
React “Hydration” only cares that the content matches during the very first render. After that we can change anything we want without issues.
So one option is to simply remove the content from the first render:
Of course, you don’t have to return null
. You can still render something if you’d like, you just need to ensure it does not contain any dynamic values.
This is a simple solution, but there are also potential pitfalls. If you would like to learn more about React Hydration errors, you can read my other blog post: