Hydration and hydrateRoot
Server rendering can send useful HTML before the browser downloads and executes the client React application.
Hydration is the phase where React connects component logic to that existing server-generated DOM.
The hydration pipeline
import { hydrateRoot } from 'react-dom/client';
import App from './App.jsx';
hydrateRoot(
document.getElementById('root'),
<App />
);
Use hydrateRoot when the container already contains HTML produced by React on the server.
For a client-only empty container, use createRoot.
createRoot vs hydrateRoot
Container starts as a client React surface.
The first root.render creates React-owned DOM.
Container already contains React server HTML.
React reuses compatible DOM and attaches behavior.
Using the wrong API changes the ownership model.
The first client render must match
Server output:
<button>Count: 0</button>
Client component:
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
}
The initial client render conceptually describes the same button, so React can connect interactivity to the existing DOM.
Hydration mismatches
Common mismatch sources include:
Date.now()orMath.random()during render;- locale/timezone differences;
- browser-only state read during the initial render;
- server/client data snapshots that disagree;
- browser extensions or third-party scripts mutating DOM first;
- invalid HTML that the browser repairs;
- feature flags/configuration that differ across environments.
function Clock() {
return <p>{Date.now()}</p>;
}
The server and browser almost certainly compute different text.
Prefer deterministic first render
For truly browser-only information, use a server-compatible initial state and synchronize after hydration.
function OnlineStatus() {
const [online, setOnline] = useState(null);
useEffect(() => {
setOnline(navigator.onLine);
}, []);
if (online === null) {
return <span>Checking connection…</span>;
}
return <span>{online ? 'Online' : 'Offline'}</span>;
}
Do not use this as a universal mismatch patch. Prefer deterministic server data when possible.
suppressHydrationWarning
<time suppressHydrationWarning>
{new Date().toLocaleTimeString()}
</time>
This is a narrow escape hatch for intentionally different content. It does not make broad server/client disagreement safe.
Hydration still performs client work
Hydration is not “running the server HTML again,” but React still needs enough client-side work to connect the DOM to the live tree.
Multiple roots and identifierPrefix
A page may have independent hydrated roots:
hydrateRoot(document.getElementById('header-root'), <Header />);
hydrateRoot(document.getElementById('app-root'), <App />);
Generated ID namespaces must remain deliberate across roots. If identifierPrefix is used, server rendering and hydration must agree on it so APIs such as useId remain consistent.
Root lifecycle after hydration
const root = hydrateRoot(container, <App />);
Later updates can use the returned root, but do not call root.render as an immediate initialization step. If it runs before hydration finishes, React can clear the server HTML and switch the root to client rendering.
root.unmount();
Unmounting matters when a non-React shell, CMS, legacy application, or micro-frontend host is about to remove a DOM region containing a React root.
Hydration + Suspense + streaming
Streaming changes hydration from an all-or-nothing mental model into boundary-aware progressive work.
This is one reason modern server rendering prefers streaming APIs over one final blocking string for Suspense-heavy applications.
SSR and Server Components are different axes
Component executes on the server to make HTML.
Its client implementation may still ship and hydrate.
Component implementation executes in the server environment.
Interactive Client Component boundaries define what browser code is needed.
SSR answers how output becomes HTML. Server Components answer where component code executes and what crosses the server/client boundary.
Data consistency is the production contract
The browser should initialize from the same logical snapshot the server used to produce HTML.
Common strategies include:
- serialized initial data;
- framework loader data;
- cache dehydration/rehydration;
- shared route params, locale, and feature flags;
- versioned data when stale responses are possible.
Browser-only APIs
This is invalid during normal server rendering:
const width = window.innerWidth;
Even a guard can create mismatched output:
const isBrowser = typeof window !== 'undefined';
return <p>{isBrowser ? 'Client' : 'Server'}</p>;
The server describes one tree while the first browser render describes another.
Debugging hydration
Do not silence mismatch warnings before understanding the cause.
Common mistakes
- using
createRootfor server-generated HTML; - rendering timestamps/random values directly during initial render;
- ignoring hydration warnings because the page “looks fine”;
- making everything client-only instead of fixing the server/client contract;
- calling
root.renderimmediately afterhydrateRoot; - assuming server HTML means hydration is free.
Exercise
Create an SSR-compatible profile page with deterministic HTML, a hydrated Like button, a useId form label, a browser-only online-status indicator that does not mismatch, and one timestamp mismatch that you deliberately diagnose and fix.
Document every input that must be identical between server and first client render.
Interview questions
Junior: What is hydration?
Mid-level: Why are hydration mismatches correctness issues rather than cosmetic warnings?
Senior: How do streaming Suspense boundaries change the traditional “render all HTML, then hydrate everything” architecture?