A hydration mismatch happens when the HTML rendered on the server differs from what the client's first render produces. React expects them to match so it can adopt the existing DOM. Typical causes: non-deterministic values (Date.now(), Math.random(), generated ids), environment branches (typeof window, localStorage, navigator, media queries) during render, locale and time-zone differences in formatting, invalid HTML nesting the browser silently repairs, different data on server and client, and browser extensions or third-party scripts modifying the DOM. Fix them by making the first client render identical and deferring client-only values to after hydration (effects, useSyncExternalStore with a server snapshot), or by using suppressHydrationWarning for small, intentional text differences such as timestamps.
<head>/<body>, and you can report recovered errors with hydrateRoot(..., { onRecoverableError }).Attribute mismatches (as opposed to text or structure) are generally not patched: React warns in development and leaves the server value in place, which can leave subtle bugs.
1. Time and randomness
// Bug: server and client render at different times
<p>Updated {new Date().toLocaleTimeString()}</p>
Fix: render a stable value (pass the timestamp from the server as data), format with an explicit locale and time zone, or render the relative time after mount. For a single text node that's legitimately different, <time suppressHydrationWarning> silences the warning (one level deep only).
2. Client-only APIs in render
// Bug
const isMobile = typeof window !== 'undefined' && window.innerWidth < 768;
// Fix: same output on the server and on the first client render, then update
const [isMobile, setIsMobile] = useState(false);
useEffect(() => setIsMobile(window.innerWidth < 768), []);
Better still, use CSS media queries for layout, or useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) where getServerSnapshot returns the server-safe value.
3. Ids: use useId, not counters or random values.
4. Invalid nesting: <p><div /></p>, <a> inside <a>, <tr> directly inside <table> without <tbody>. The parser restructures the HTML, so the DOM no longer matches. Fix the markup.
5. Data drift: the client fetches fresh data before hydrating instead of using the serialized server data. Hydrate with the same payload (frameworks handle this) and refetch after.
6. Locale and user state: cookies or Accept-Language-based formatting must use the same inputs on both sides. Pass locale and time zone explicitly.
7. Extensions and third-party scripts: React 19 is more tolerant at the document level. Otherwise, render those regions only on the client.
For widgets that can't render on the server (maps, canvas editors), render a placeholder on the server and the real component after mount, or use a framework's ssr: false dynamic import. Wrap them in Suspense so they don't block hydration.
window, locale, nesting, data.useSyncExternalStore server snapshots, useId, suppressHydrationWarning sparingly.onRecoverableError, because mismatches cost performance silently.Spotted a mistake or a rights concern? Report an issue