Hydration and Server-Client Consistency
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Explain what hydration attaches to server HTML.
- Identify the common causes of hydration mismatches.
- Make a component hydration-safe by deferring client-only reads.
Overview
#Hydration attaches behavior
Server rendering sends HTML, and hydration makes that HTML interactive by attaching React's event handlers and state to the existing DOM. For hydration to work, the first client render must produce the same output as the server HTML. If it does not, React reports a mismatch.
What causes mismatches
Anything that differs between server and client on the first render causes a mismatch: the current time, Math.random, locale formatting, a value read from localStorage or window, or a browser-only branch.
// mismatch: server and client compute different values
return <p>{Date.now()}</p>;
Fixing mismatches
The fix is to render the same thing on both sides for the first render, then update on the client after hydration. Move client-only values into an effect, or gate them behind a mounted flag.
const [now, setNow] = useState<number | null>(null);
useEffect(() => setNow(Date.now()), []);
return <p>{now ?? 'loading'}</p>;
suppressHydrationWarning
For a genuinely unavoidable difference in a single text node, suppressHydrationWarning silences the warning for that element and its children. Use it sparingly; it hides the symptom, not the cause.
Selective hydration and streaming
React can hydrate parts of the page as their code and data arrive, rather than all at once. Suspense boundaries define the units. This lets the user interact with ready parts while the rest streams in.
Pitfall: reading browser APIs during render
Reading window, localStorage or matchMedia during render produces different output on the server. Read them in an effect or use useSyncExternalStore with a server snapshot.
Recoverable errors
React 19 reports recoverable hydration errors through onRecoverableError. React will fall back to client rendering for the affected subtree, which fixes the UI but costs performance, so treat these errors as bugs to fix.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does React do when the first client render differs from the server HTML?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsxreturn <p>{Date.now()}</p>;Exercise 2Implement
Make this component hydration-safe by reading the time only after mount.
starter.tsxexport function Clock() { return <p>{new Date().toLocaleTimeString()}</p>; }Reveal the reference answer
reference.tsximport { useEffect, useState } from 'react'; export function Clock() { const [time, setTime] = useState<string | null>(null); useEffect(() => { const id = setInterval(() => setTime(new Date().toLocaleTimeString()), 1000); return () => clearInterval(id); }, []); return <p>{time ?? '--:--:--'}</p>; }Authored expected output: The server and first client render show the placeholder; the real time appears after mount.
The initial render is identical on both sides because it uses a constant placeholder. The effect runs only on the client after hydration and fills in the time.
Exercise 3Debug
This component mismatches on hydration. Explain and fix.
starter.tsxconst theme = localStorage.getItem('theme') ?? 'light'; return <div data-theme={theme}>...</div>;Reveal the reference answer
reference.tsxconst [theme, setTheme] = useState('light'); useEffect(() => { setTheme(localStorage.getItem('theme') ?? 'light'); }, []); return <div data-theme={theme}>...</div>;Authored expected output: The server and first client render both use light; the stored theme applies after mount.
localStorage does not exist on the server, so the server renders a different value than the client. Rendering a constant first and reading storage in an effect keeps the first render consistent.
Exercise 4Interview
Explain hydration to a teammate and describe how you would debug a mismatch.
starter.tsx// server HTML, client render, mismatch reportReveal the reference answer
reference.tsxHydration is React attaching event handlers and state to server-rendered HTML so it becomes interactive. For it to work, the first client render must match the server HTML. To debug a mismatch, read the reported difference in the console, which shows the server and client markup, then find the value that differs. Common causes are current time, random values, locale formatting, and browser-only APIs read during render. Fix by rendering the same value on both sides and moving client-only reads into an effect or a store with a server snapshot.Authored expected output: A clear definition plus a concrete debugging method and the common causes.
The answer should connect the concept to the workflow: read the diff, find the divergent value, and make the first render deterministic.
Common misconceptions
#- Hydration re-renders everything from scratch. It attaches to existing HTML; a mismatch forces a client re-render of the subtree.
- suppressHydrationWarning fixes the cause. It only silences the warning for one element.
- Reading localStorage during render is fine. It differs on the server and causes a mismatch; read it in an effect.
- Hydration mismatches are harmless. They force a client re-render and can break interactivity.
Follow-up practice
#- react-concurrent-rendering
- react-component-testing
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.