Skip to main content

What is hydration?

Hydration is the process in which React "brings to life" the already-ready HTML received from the server, attaching JavaScript and event handlers to it.

In short:

HTML already exists → React attaches → the page becomes interactive

How hydration works (step by step)

  1. The server sends ready HTML
html
<button>Click me</button>
  1. The browser immediately shows the content (the page is visible, but "dead")

  2. The JavaScript bundle loads

  3. React:

  • matches the HTML with the Virtual DOM
  • attaches onClick, onChange, etc.
  1. The page becomes interactive
HTML → JS → hydrate → interactivity

Hydration vs Render

RenderHydration
Whereserverbrowser
HTMLis createdalready exists
JSNoYes
EventsNoYes
InteractivityNoYes

When hydration happens

Hydration happens if:

  • SSR is used
  • SSG is used
  • ISR is used

There is no hydration if:

  • plain HTML (without React)
  • an API response
  • middleware

Hydration in the App Router

In the App Router:

  • Server Components are NOT hydrated
  • Client Components - are hydrated
txt
Page (Server) ├─ Header (Server) no hydration ├─ Content (Server) no hydration └─ Chat (Client) with hydration

This is a key Next.js optimization.


Client Component example (gets hydrated)

tsx
"use client"; export function Counter() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>{count}</button>; }
  • the HTML arrives from the server
  • the JS attaches
  • the button starts working

Hydration mismatch (a common error)

What is it?

When the HTML from the server does not match what React expects on the client.

Causes:

  • Math.random()
  • Date.now()
  • window, localStorage
  • conditional rendering based on typeof window
tsx
// bad <div>{Math.random()}</div>

How to avoid hydration mismatch

  • move dynamic values into useEffect
  • use use client only where needed
  • check mounted
  • do not use browser-only APIs in Server Components

Streaming and partial hydration

Next.js can:

  • stream HTML
  • hydrate components as JS loads

This:

  • speeds up the first screen
  • lowers TTI
  • improves UX

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.