Hydration Mismatches
Why server HTML must match the client’s first render: common causes, useId, dates, and suppressHydrationWarning.
- react
- hydration-mismatches
With SSR or RSC + client hydration, React attaches to existing HTML. If the client’s first render tree does not match the server markup, you get a hydration mismatch — warnings in dev, discarded server DOM, and potential UI glitches or security surprises.
Docs: hydrateRoot, useId.
Common causes
// Bad: different on server vs client
function Clock() {
return <span>{new Date().toLocaleString()}</span>;
}
function RandomBadge() {
return <span>{Math.random()}</span>;
}
function BrowserOnly() {
return <span>{window.innerWidth}</span>; // window missing on server
}
Also: invalid HTML nesting (<p><div/></p>) that the browser “fixes” before React hydrates, extension-injected DOM, and locale/timezone differences.
Fixes
1. Same deterministic output for the first client render as the server.
2. Client-only after mount for inherently browser values:
function Width() {
const [w, setW] = useState(null);
useEffect(() => setW(window.innerWidth), []);
if (w === null) return <span>—</span>; // server + first client paint
return <span>{w}px</span>;
}
3. useId for ids instead of random counters.
4. suppressHydrationWarning only on specific text nodes you accept (e.g. timestamps) — not a blanket ignore on the app root:
<time suppressHydrationWarning>{new Date().toLocaleString()}</time>
Next.js notes
Client Components still SSR by default. "use client" does not mean “client only.” Use dynamic(..., { ssr: false }) sparingly for true browser-only widgets. Mismatches often come from localStorage reads during render.
Interview out-loud
“Hydration requires the client’s first render to match server HTML. I avoid Date.now, random, and window during render, use useId for ids, defer browser-only values to effects, and reserve suppressHydrationWarning for tiny intentional mismatches.”
Related on this site
- useId for Accessibility IDs
- Server Components Overview
- Client Component Boundaries
- Strict Mode Double Effects
Further reading
Edge cases worth rehearsing
Interviewers and production incidents cluster around the same edges: first render versus update, empty and loading states, Strict Mode double setup, concurrent interruptions, and what happens when identity (key, route params, user id) changes mid-edit. Walk one concrete user journey end-to-end — open, edit, navigate away, come back — and say which state survives.
Prefer fixing data flow and ownership before reaching for memoization or micro-optimizations. Prefer event handlers over effects when a user action is the trigger. Prefer deriving values during render over mirroring props into state. Prefer stable list keys from business ids. Measure with the profiler when performance is the claim.
When you cite an API, mention one failure mode: abort on unmount, serializable props across server/client boundaries, focus restoration for dialogs, or cache invalidation after a mutation. Specific beats generic every time.
Edge cases worth rehearsing
Interviewers and production incidents cluster around the same edges: first render versus update, empty and loading states, Strict Mode double setup, concurrent interruptions, and what happens when identity (key, route params, user id) changes mid-edit. Walk one concrete user journey end-to-end — open, edit, navigate away, come back — and say which state survives.
Prefer fixing data flow and ownership before reaching for memoization or micro-optimizations. Prefer event handlers over effects when a user action is the trigger. Prefer deriving values during render over mirroring props into state. Prefer stable list keys from business ids. Measure with the profiler when performance is the claim.
When you cite an API, mention one failure mode: abort on unmount, serializable props across server/client boundaries, focus restoration for dialogs, or cache invalidation after a mutation. Specific beats generic every time.
Related guides
- useCopyToClipboard HookCopy text with navigator.clipboard, fallback execCommand, transient copied state, and permissions that fail in insecure contexts.
- useDebouncedValue HookDebounce a changing value in React without firing effects on every keystroke. Cleanup, leading vs trailing, and the stale timer that updates unmounted state.
- useEventListener HookAttach a window, document, or element listener with a stable handler ref so you do not rebind on every render. Capture option and target changes.
- useFetch With AbortControllerFetch in an effect with a signal, ignore aborted errors, and prevent the slow response from winning a race after the url changes.
- useIdle HookDetect idle users: reset a timer on input events, fire idle after timeout, and decide whether mount starts the timer immediately.