Fix "Hydration failed because the server rendered HTML didn’t match the client" (React, Next.js)
Error: Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client.
- Error: Hydration failed because the initial UI does not match what was rendered on the server.
- Error: Text content does not match server-rendered HTML.
- Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.
- A tree hydrated but some attributes of the server rendered HTML didn't match the client properties. This won't be patched up.
- In HTML, <div> cannot be a descendant of <p>. This will cause a hydration error.
The HTML generated on the server and React’s first render in the browser are not identical. Most often, a component renders a browser-specific value (Date, Math.random(), window, localStorage) during render, or the HTML is badly nested (<div> inside a <p>). Move that code into a useEffect and fix the nesting.
What this error means
With server rendering (Next.js, Remix, React Router, renderToString), the server sends already filled HTML, then React "hydrates" the page: it renders again in the browser and attaches events to the existing DOM instead of recreating it. For this, the first client render must produce exactly the same tree as the server. If a text, attribute or tag differs, React 18 and 19 give up hydrating that tree and regenerate it on the client: the page flickers, loses performance and can show inconsistent content. React 19 and Next.js 15+ print a diff of the offending lines in the console and the development overlay.
Common causes
- A value that changes on every render:
new Date(),Date.now(),Math.random()or a random id computed during render give a different result on the server and in the browser. - Browser-only code in render: a
typeof window !== 'undefined'check,localStorage,navigatoror the screen size produce content the server cannot know. - Invalid HTML: a
<div>or<p>inside a<p>, an<a>inside an<a>, a<tr>without<tbody>. The browser fixes the DOM its own way, which no longer matches the React tree. - Dates and time zones:
toLocaleString()depends on the locale and time zone, which differ between the server (often UTC) and the user. - Browser extension: translators, password managers or grammar checkers add attributes to
<html>or<body>before hydration.
Step-by-step fix
-
Read the diff and test without extensions
In the console or the Next.js overlay,
+lines show the client render and-lines the server HTML: they point to the faulty element. Then open the page in a private window, without extensions: if the error disappears, an extension is the cause and your code is not at fault. -
Fix the HTML nesting
A
<p>can only contain inline content. Replace the parent<p>with a<div>, or the block child with a<span>. Also check library components that render a<p>(for example aTypographycomponent).// Before: <p><div>Price</div></p> <div className="price"> <div>Price</div> </div> -
Move browser code into
useEffectuseEffectonly runs in the browser, after hydration. First render the same value as the server (nullor neutral text), then update it in the effect.'use client'; import { useEffect, useState } from 'react'; export default function LocalTime() { const [time, setTime] = useState(null); useEffect(() => { setTime(new Date().toLocaleTimeString()); }, []); return <span>{time ?? '--:--'}</span>; } -
Use
useIdfor identifiersAn
idgenerated withMath.random()differs between server and client.useIdproduces a stable id that is the same on both sides.import { useId } from 'react'; function EmailField() { const id = useId(); return ( <> <label htmlFor={id}>Email</label> <input id={id} type="email" /> </> ); } -
Next.js: load a component on the client only
For a component that fully depends on the browser (map, editor, chart), disable its server render with
next/dynamic. In the App Router,ssr: falsemust be used in a file marked'use client'.'use client'; import dynamic from 'next/dynamic'; const Map = dynamic(() => import('./Map'), { ssr: false }); export default function MapSection() { return <Map />; } -
Last resort:
suppressHydrationWarningFor an unavoidable and harmless difference (a timestamp, a theme attribute added to
<html>bynext-themes), this attribute silences the warning on that element only, one level deep. It fixes nothing: do not use it to hide a real content mismatch.<html lang="en" suppressHydrationWarning> <body>{children}</body> </html>
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Never read
window,localStorage, the current date or a random value directly in the render of a server-rendered component: go throughuseEffector receive the value as props from the server. - Format dates with an explicit locale and time zone, for example
Intl.DateTimeFormat('en-US', { timeZone: 'UTC' }), to get the same text everywhere. - Fix every HTML nesting warning ("cannot be a descendant of") as soon as it shows up in development.
Frequently asked questions
Can a Chrome extension cause a hydration error?
Yes. Extensions that change the DOM before React hydrates (translation, grammar checker, password manager) create a mismatch, often on <html> or <body>. Test in a private window to confirm.
Why doesn’t typeof window !== 'undefined' fix the error?
Because that check is false on the server and true in the browser: the two renders differ precisely because of it. The first client render must match the server; use useEffect to show the browser version afterwards.
Is a hydration error serious in production?
The page displays, but React rebuilds the affected tree on the client: lost performance, flicker and sometimes inconsistent state. Fix it rather than ignore it.
Useful tools for this error
More JavaScript & Node.js errors
- Fix "Cannot read properties of undefined" in JavaScript
- Fix "blocked by CORS policy: No 'Access-Control-Allow-Origin' header"
- Fix "Unexpected token '<', "<!DOCTYPE "... is not valid JSON"
- Fix "Error: Cannot find module" in Node.js
- Fix "npm ERR! ERESOLVE unable to resolve dependency tree"
- Fix "Too many re-renders. React limits the number of renders" in React
- Fix "Error: listen EADDRINUSE: address already in use" in Node.js
- Fix "SyntaxError: Cannot use import statement outside a module"