Skip to main content

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​

Hydration reuses server HTML instead of rebuilding correct DOM from scratch
Server React tree
HTML
Browser already has DOM
React connects logic + events to existing DOM
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​

Choose the root API based on who already owns the DOM
CLIENT-OWNED SURFACEcreateRoot

Container starts as a client React surface.
The first root.render creates React-owned DOM.

SERVER HTML EXISTShydrateRoot

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 is a consistency contract
Server snapshot
Props · route params · data · locale · feature flags · generated IDs
First client snapshot
Must describe the same initial UI and identifiers
Compatible existing DOM becomes interactive

Hydration mismatches​

Common mismatch sources include:

  • Date.now() or Math.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>;
}
Two-pass browser-only UI has a real cost
1Server renders stable placeholder
2Browser hydrates same placeholder
3Effect reads browser environment
4React renders browser-specific UI

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​

Existing HTML does not mean zero client cost
Already available
HTML and DOM can be visible before React finishes loading.
Still required
Download JS · evaluate components · recreate React tree · attach events · hydrate interactive regions.

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.

External hosts must respect the React root lifecycle
External host owns outer region
Hydrated React root owns its subtree
root.unmount() lets React clean up

Hydration + Suspense + streaming​

Streaming changes hydration from an all-or-nothing mental model into boundary-aware progressive work.

Streaming and hydration cooperate across Suspense boundaries
1Server sends shell
2Fallbacks represent pending boundaries
3Completed HTML streams later
4React hydrates available interactive regions
5Page becomes progressively interactive

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​

Do not equate server rendering with Server Components
Traditional SSR

Component executes on the server to make HTML.
Its client implementation may still ship and hydrate.

Server Component

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​

A hydration mismatch appeared. What should you compare?
Render inputsCompare props, params, data, locale, timezone, flags, IDs, random/time values
Browser-only readsCheck window/document/storage/media/locale branches during first render
HTML structureInspect actual browser DOM for repaired invalid markup
External mutationCheck extensions, analytics, legacy scripts, or libraries that touched DOM first

Do not silence mismatch warnings before understanding the cause.

Common mistakes​

  • using createRoot for 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.render immediately after hydrateRoot;
  • 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?

Summary​

Hydration in one picture
1Server renders deterministic HTML
2Browser displays existing DOM
3Client first render matches server snapshot
4hydrateRoot connects logic
5Interactive React tree continues from existing DOM

References​