Troubleshooting guide
React hydration error: causes and how to fix it
React rendered HTML on the server and then found that the browser's first render was different. Non-deterministic rendering and invalid HTML are the two usual causes.
3 min read10 sectionsUpdated
On this page
Diagnose it
The development build shows a diff. To reproduce a production issue, compare the raw HTML with what the page holds after hydration.
# HTML as the server sent it
curl -s https://example.com/page > /tmp/server.html
# Common invalid nesting to look for in that HTML
grep -oE '<p[^>]*>[^<]*<div' /tmp/server.html | head
# Check for proxy rewriting of the page
curl -sI https://example.com/page | grep -iE 'server|cf-'
grep -c 'email-decode' /tmp/server.htmlFixes
Render time-dependent values in useEffect, after the first render. Use a fixed locale and time zone when formatting on both sides. Fix nesting, such as a block element inside a paragraph. Use useId rather than random IDs. For truly client-only components, use dynamic import with ssr: false. Use suppressHydrationWarning only on one element where a difference is known and harmless, such as a timestamp.
Why the error appears only in production
Proxies and CDNs are the classic reason. A feature that rewrites HTML on its way to the browser, such as email address obfuscation or automatic minification, changes the markup after React produced it. Local testing has none of those, so the bug appears only on the live site. Browser extensions create mismatches too, which is why errors reported from real users often include some you cannot reproduce. Test in a clean private window first, and compare the raw response before you start changing components.
Catch it automatically
Hydration errors appear only in real browsers, so real-user monitoring is the natural tool: it captures them with the page and browser. React performance monitoring covers the rest.
Does a hydration error break the page?
Often it recovers by re-rendering on the client, but that costs performance.
Should I use suppressHydrationWarning everywhere?
No. It only applies one level deep and can hide real bugs.
Can a browser extension cause it?
Yes. Test in a private window with extensions off.
Related reading
- ERR_CONTENT_DECODING_FAILED: how to fix it: Fix ERR_CONTENT_DECODING_FAILED: wrong Content-Encoding headers, double gzip compression, broken Brotli, PHP output buffering and proxies, with tests.
- 422 Unprocessable Entity: how to fix it: Fix 422 Unprocessable Entity (Unprocessable Content): validation errors in APIs and forms, CSRF tokens in Rails and Laravel, and how to read the error body.
- 501 Not Implemented: how to fix it: Fix 501 Not Implemented: unsupported HTTP methods, servers and proxies that do not support PATCH or WebDAV, and how it differs from 405 and 500.
- 502 Bad Gateway: causes and how to fix it: What a 502 Bad Gateway means, the common causes behind Nginx, load balancers and CDNs, how to find yours and how to stop it happening again.
- 408 Request Timeout: causes and how to fix it: Understand 408 Request Timeout: slow client uploads, idle keep-alive connections and proxy settings, and how it differs from a 504 Gateway Timeout.
- ERR_HTTP2_PROTOCOL_ERROR: causes and how to fix it: Fix ERR_HTTP2_PROTOCOL_ERROR in Chrome: invalid response headers, crashed backends behind HTTP/2 proxies, buffer limits and antivirus interference.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.