Skip to main content

Browser · 6 common causes

Hydration failed

Updated

React's client render produced different markup than the server sent, so it discarded the server HTML and re-rendered from scratch.

The exact message

Uncaught Error: Hydration failed because the server rendered HTML didn't match the client.
As a result this tree will be regenerated on the client.

  <Clock>
    <span>
+     14:02:41
-     14:02:39

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Hydration failed.

How to fix it

Cause 1 of 6

Rendering a value that differs between runs — Date.now(), new Date(), Math.random(), crypto.randomUUID().

Keep it out of the first render. Hold the value in state and set it inside useEffect, so the server and the first client render agree and the real value appears immediately after mount. For a single unavoidable timestamp, suppressHydrationWarning on that one element is acceptable.

Cause 2 of 6

Reading browser-only APIs during render — window, document, localStorage, matchMedia.

None of them exist on the server, so the two renders take different branches. Read them in useEffect and render the server-safe default first.

Cause 3 of 6

Locale or timezone-dependent formatting.

toLocaleString() uses the server's locale and timezone on the server and the visitor's in the browser, so the strings differ. Pass explicit options such as { timeZone: 'UTC' }, or format after mount.

Cause 4 of 6

Invalid HTML nesting that the browser silently repairs.

A <div> inside a <p>, or a <tr> outside a <tbody>, is rewritten by the HTML parser before React ever sees it — so the DOM cannot match. React's error overlay names the offending tag; fix the nesting rather than the component logic.

Cause 5 of 6

A browser extension modified the DOM before hydration.

Password managers, translation tools and ad blockers inject attributes into inputs and body. Reproduce the error in a private window with extensions disabled before hunting through your own code.

Cause 6 of 6

State is restored from localStorage during the first render.

Render the server-safe default, then apply the stored value in an effect. Theme toggles are the classic case — a small inline script that sets a class on documentElement before React boots avoids both the mismatch and the flash.

Stopping it coming back

Treat the first render as something that must be identical on both sides, and put anything that depends on the browser, the clock or the user's machine into an effect.

Hydration failed — common questions

Can I just ignore the warning?
Not safely. React throws away the server HTML for that subtree and re-renders it on the client, which costs the performance benefit of server rendering and can produce a visible flash. It also frequently indicates a real bug in what the user is shown.
What does suppressHydrationWarning actually do?
It tells React not to warn about the text content of one element. It does not fix the mismatch — it just accepts it. Use it for a timestamp or a random id you cannot avoid, never on a wrapper covering a whole tree.
How do I find which element mismatched?
Recent React versions print a diff of the offending subtree in the console, marking the server and client values. If that is not enough, bisect by commenting out sections, and check any component that reads the clock or the browser during render first.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor