ReactMedium

What is Hydration?

Hydration is the process where React, running in the browser, takes HTML that was rendered on the server, rebuilds the component tree, and attaches event handlers and state to the existing DOM instead of recreating it. After hydration the static page becomes a fully interactive React app.

How it works

  • hydrateRoot(container, <App />) from react-dom/client (React 18+; ReactDOM.hydrate was removed in React 19).
  • React renders the same tree on the client and expects it to match the server HTML. It reuses DOM nodes and wires up listeners.
  • With streaming SSR, React performs selective hydration: each <Suspense> boundary hydrates as its HTML and code arrive, and boundaries the user clicks are hydrated first.

Hydration mismatches

If the client's first render differs from the server HTML, React logs a hydration error and re-renders that part on the client. Common causes:

  • Date.now(), Math.random(), locale or time-zone differences.
  • Branching on typeof window during render.
  • Reading localStorage during render.
  • Invalid HTML nesting (<div> inside <p>).
  • Browser extensions modifying the DOM.

React 19 shows a single, clearer diff of the mismatch.

Fixes

  • Render the same output on both sides, then update in useEffect.
  • useSyncExternalStore with a server snapshot.
  • suppressHydrationWarning for single unavoidable text differences (timestamps).
  • React 19.3's use(browser()) (from react-dom) suspends during server rendering so a component renders only in the browser.

Server Components

Server Components are not hydrated; only Client Components are, which reduces hydration cost.

Code example

tsx
1// client.tsx
2import { hydrateRoot } from "react-dom/client";
3import { useEffect, useState } from "react";
4
5function LocalTime({ iso }: { iso: string }) {
6 // First client render must match server HTML: format in a fixed time zone
7 const [text, setText] = useState(() => new Date(iso).toLocaleString("en-US", { timeZone: "UTC" }) + " UTC");
8
9 useEffect(() => {
10 // After hydration, switch to the user's local time
11 setText(new Date(iso).toLocaleString());
12 }, [iso]);
13
14 return <time dateTime={iso}>{text}</time>;
15}
16
17function App({ publishedAt }: { publishedAt: string }) {
18 return (
19 <article>
20 <h1>Release notes</h1>
21 <LocalTime iso={publishedAt} />
22 <button onClick={() => alert("hydrated!")}>Interactive after hydration</button>
23 </article>
24 );
25}
26
27hydrateRoot(document.getElementById("root")!, <App publishedAt="2026-09-09T17:00:00Z" />, {
28 onRecoverableError: (err) => console.warn("hydration issue", err),
29});

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app