What Hydration Is: Attaching Event Listeners to Server HTML
What you'll learn: How React transforms static server-rendered HTML into a fully interactive application on the client side.
The Core Problem
When you use renderToString or renderToPipeableStream, the server generates HTML and sends it to the browser. The user sees content immediately, but there's a catch: this HTML has no event listeners attached. Click a button, type in an input—nothing happens. The page looks alive but is actually frozen.
What Hydration Does
Hydration is the process where React "wakes up" the static HTML on the client. Think of it like pouring water on a freeze-dried meal—the structure is already there, but you're bringing it back to life.
Specifically, hydration:
- Loads your JavaScript bundle in the browser
- Recreates the React component tree in memory
- Attaches event listeners to the existing DOM nodes
- Initializes state and context
- Makes the app interactive without replacing the HTML
The Critical Insight
React doesn't rebuild the DOM from scratch. Instead, it says: "I expect this HTML to already exist based on my components. Let me find those nodes and wire them up."
This is why hydration errors occur when server HTML doesn't match what React expects on the client—React can't confidently attach event handlers if the structure is different.
The Process Analogy
Imagine a stage play where the sets are built beforehand (server rendering), but the actors arrive later to bring it to life (hydration). The set doesn't change; the actors just step into their positions and start performing.
Key Takeaway: Hydration transforms server-rendered static HTML into a fully interactive React application by attaching event listeners and initializing state, without rebuilding the DOM structure.