Fixing React Hydration Errors
What hydration errors mean and how to fix them. The most common cause of 'content does not match' errors in Next.js and React SSR apps.
What is hydration?
When you use server-side rendering (SSR), React first renders your component on the server as HTML. The browser displays this HTML immediately (fast initial load). Then React 'hydrates' - it attaches event handlers and makes the HTML interactive. Hydration errors occur when the server HTML doesn't match what React tries to render on the client.
Common causes
Using browser-only APIs during render: Date.now(), window.innerWidth, localStorage. These don't exist on the server, so the server renders different content than the client. Conditional rendering based on auth state that's only available client-side. Using random values or time-dependent content. HTML nesting violations (like a <p> inside a <p>).
How to fix them
For browser-only values: render a default on the server and update client-side with useEffect. For auth-dependent content: use Suspense boundaries or loading states. For time-dependent content: use suppressHydrationWarning on the specific element (last resort). For HTML violations: fix the nesting - use the browser's DOM inspector to find invalid structures.
The useEffect pattern
The safest way to render browser-only content: start with a server-safe default, then update after mount. Use a state variable initialized to the safe default, and update it in useEffect. This ensures the server and client render the same content initially, then the client updates to the correct value.
Need help with this?
Our team handles fix bugs for AI-built apps every day. Get a fixed quote within 24 hours.
Start with a self-serve audit
Get a professional review of your app at a fixed price.
Security Review
Automated Security Scan
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedSecurity Review
Manual Security Review
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteSecurity Review
Full Pentest
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
Fix Bugs
Code Audit
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedFix Bugs
Bug Fixing
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteFix Bugs
Ongoing Support
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
Refactor Code
Code Audit
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedRefactor Code
Refactoring
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteRefactor Code
Full Rewrite
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
100% of your audit purchase is credited toward any paid service. Start with an audit, then let us fix what we find.
Related technologies
Need help with your app?
Tell us about your project. We'll respond within 24 hours with a clear plan and fixed quote.