Sub topic 3
What hydration does to server HTML
This chapter assumes you know function components, useState and useEffect, basic HTML and the DOM, and that a server can return an HTML string. It covers what the browser does with server-rendered HTML and what React then does to it. It does not cover the hydrateRoot call itself or mismatches, which come in later chapters.
HTML that does nothing yet
When a server sends rendered HTML, the browser can paint it right away. The page looks finished. But the HTML is only markup. No React code has run in the browser, so no event handlers are attached and no state exists. A button on the page shows up and does nothing when clicked.
Hydration is the step that fixes this. React runs your components in the browser, compares the result to the DOM that the server produced, and attaches event handlers and state to the existing nodes. React does not rebuild the page. It reuses the nodes that are already there.
What React does during hydration
React performs these steps when the JavaScript bundle loads:
- It renders your component tree in memory, as it would for any client render.
- It walks the existing DOM alongside that result and expects the two to match.
- It attaches event handlers to the matching DOM nodes.
- It starts the normal lifecycle. State is live, and effects run.
The key assumption is in step 2. React expects the first client render to produce the same output as the server did. It does not diff and repair every difference. If the output differs, you get a hydration mismatch, which is the subject of a later chapter.
Why the order of work matters
Two details explain most hydration behavior.
First, effects do not run on the server. useEffect runs only in the browser, after hydration. Code in an effect can therefore safely use window or read the current time, because it runs after the matching step is finished.
Second, the page has two separate moments: when it becomes visible and when it becomes interactive. The gap between them can be noticeable on slow devices or large bundles. A user may click in that gap and see nothing happen. Streaming and selective hydration, covered later, shorten it.
A small example
Consider this component.
import { useState } from 'react';
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}The server renders this to <button>Clicked 0 times</button>. The onClick prop is not part of the HTML. After hydration, React has attached the handler to that same button, and clicking it updates the text. The button element in the DOM is the one the server created.
Exercise
A user loads a server-rendered page and clicks a button before the JavaScript bundle has finished loading. Why does nothing happen, and what is the button's state after hydration completes?
Show answer
The server HTML contains only markup, with no event handlers, because handlers are attached only when React hydrates in the browser. After hydration, React has attached the handler to the same existing button node, and state starts from its initial value (for example, 0), so earlier clicks are not replayed.