How to Fix AI Creating React Hydration Mismatches
AI agents generate React components that render different HTML on the server and client, triggering hydration errors and broken UI on first load.
The agent writes components that produce different HTML on the server versus the
client — dates, random values, window checks — causing React to throw a
hydration mismatch error and re-render the entire tree on load.
The symptom
A component reads Date.now(), Math.random(), or window during render,
producing different output server-side vs. client-side.
// WRONG — hydration mismatchexport function Timestamp() { return <p>Page loaded at: {new Date().toLocaleTimeString()}</p>; // Server renders "10:00:00 AM", client renders "10:00:01 AM" — mismatch}
// WRONG — conditional on windowexport function ThemeIcon() { const isDark = typeof window !== "undefined" && window.matchMedia("(prefers-color-scheme: dark)").matches; return <span>{isDark ? "🌙" : "☀️"}</span>; // Server: always renders "☀️"; client: may render "🌙" — mismatch}The browser console shows:
Error: Hydration failed because the initial UI does not match what was renderedon the server.Why it happens
The agent writes components that seem correct in isolation. It doesn’t simulate two separate render passes (SSR + client hydration) and doesn’t know that any value that differs between the two will break hydration.
How to spot it
Date.now(),new Date(),Math.random()called directly in JSX or during render without being inside auseEffectoruseStateinitializer.typeof window !== "undefined"guards inside the render return — the server always takes thefalsebranch.localStorage,sessionStorage, ornavigatorread during render.- Mismatched HTML tags or attribute values between server and client output (visible in the React DevTools hydration warning).
How to fix it
Defer browser-only values to after mount, or generate stable values server-side and pass them as props.
// CORRECT — defer client-only value to after mount"use client";import { useState, useEffect } from "react";
export function Timestamp() { const [time, setTime] = useState<string | null>(null);
useEffect(() => { setTime(new Date().toLocaleTimeString()); }, []);
if (!time) return <p>Loading time…</p>; // matches server output return <p>Page loaded at: {time}</p>;}// CORRECT — theme icon: use CSS / data attribute, avoid JS render branch// Set data-theme on <html> in a blocking inline script (not React)// Then use CSS: [data-theme="dark"] .icon { content: "🌙" }
// Or use Next.js next-themes which handles SSR safely:import { useTheme } from "next-themes";import { useEffect, useState } from "react";
export function ThemeIcon() { const { resolvedTheme } = useTheme(); const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); if (!mounted) return <span style={{ width: 24 }} />; // stable placeholder return <span>{resolvedTheme === "dark" ? "🌙" : "☀️"}</span>;}[ ] No Date.now() / Math.random() / new Date() called directly during render[ ] No typeof window / localStorage / navigator accessed during render[ ] Browser-only state initializes to null/undefined, set in useEffect[ ] Render a stable placeholder (same as server output) until after mount[ ] Use suppressHydrationWarning only on elements where the mismatch is intentional (e.g. timestamp)[ ] Run "next build && next start" and check browser console for hydration errorsFix Prompt
This component causes a React hydration mismatch because it reads browser-onlyvalues (Date, window, localStorage, Math.random) during render. Fix it: moveall browser-only reads into a useEffect that sets state after mount, render astable placeholder on first render that matches what the server produces, andnever conditionally return different JSX trees based on typeof window. Explainwhy each change prevents the mismatch.Test
# Build and start, then check for hydration errors with curl diffnext build 2>&1 | grep -i "hydrat\|mismatch" && echo "FAIL: hydration error in build" || echo "Build OK"
# Runtime: open browser console after next start and look for hydration warningsnext start &sleep 3curl -s http://localhost:3000 | grep -i "data-nextjs-scroll-focus-boundary" > /dev/null && echo "Server rendered OK"