Next.jsMedium

How does React handle hydration in Next.js?

Hydration is the step where React, in the browser, attaches event handlers and state to HTML that was already rendered on the server, turning static markup into an interactive app. In Next.js only Client Components hydrate; Server Component output stays as plain HTML.

The sequence on first load

  1. The server renders Server and Client Components to HTML and streams it, together with the RSC payload.
  2. The browser paints the HTML immediately, so content is visible before JavaScript runs.
  3. JavaScript for the Client Components downloads.
  4. React rebuilds the tree from the RSC payload, matches it against the existing DOM, and attaches listeners and state (hydrateRoot).
  5. Effects run; the page is interactive.

Streaming and selective hydration

With <Suspense>, React hydrates each boundary as soon as its HTML and code arrive, and it prioritizes boundaries the user interacts with. A slow widget does not delay interactivity elsewhere.

Why Server Components help

Server Component code is never hydrated or downloaded, so hydration work shrinks to the interactive islands. That improves Interaction to Next Paint (INP) and Total Blocking Time.

Hydration errors

React expects the first client render to match the server HTML. Differences (dates, random values, window checks, invalid HTML nesting, browser extensions changing the DOM) trigger a hydration error. Next.js 16.2 shows a + Client / - Server diff in the error overlay.

After hydration

Navigations do not hydrate again; React applies new RSC payloads to the live tree. Pages left behind can be kept hidden in <Activity> with Cache Components.

Code example

tsx
1// app/clock.tsx - hydration-safe time display
2"use client";
3
4import { useEffect, useState } from "react";
5
6export function Clock({ serverTime }: { serverTime: string }) {
7 // First client render matches the server HTML exactly
8 const [time, setTime] = useState(serverTime);
9
10 useEffect(() => {
11 // Runs only after hydration, in the browser
12 setTime(new Date().toLocaleTimeString());
13 const id = setInterval(() => setTime(new Date().toLocaleTimeString()), 1000);
14 return () => clearInterval(id);
15 }, []);
16
17 return <time suppressHydrationWarning>{time}</time>;
18}
19
20// app/page.tsx (Server Component)
21// import { connection } from "next/server";
22// await connection();
23// <Clock serverTime={new Date().toLocaleTimeString("en-US", { timeZone: "UTC" })} />

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 Next.js set.

Open in study app