React Hydration is a key concept when working with Server-Side Rendering (SSR) or Static Site Generation (SSG) in React frameworks like Next.js.
Let’s unpack it clearly 👇
🧠 What is Hydration in React?
Hydration is the process where React attaches event listeners and reactivates components on the client side after the HTML is already rendered on the server.
In simple words:
🧩 The server renders HTML → browser receives and shows it → React loads JavaScript → React “hydrates” that HTML, making it interactive.
🧩 Example
1️⃣ Server renders HTML
Imagine your server sends this pre-rendered HTML to the browser:
<div id="root">
<button>Clicked 0 times</button>
</div>
2️⃣ React hydrates it
Then, React runs in the browser and attaches the component logic — so clicking the button now works.
import React from "react";
import { hydrateRoot } from "react-dom/client";
import App from "./App";
hydrateRoot(document.getElementById("root"), <App />);
Now the HTML is “alive” — React knows which components are where, and event handlers like onClick start working.
⚙️ How it Differs from Normal Rendering
| Rendering Type | Function Used | Description |
|---|---|---|
| Client-Side Rendering (CSR) | createRoot().render(<App />) |
React builds everything in the browser. |
| Server-Side Rendering (SSR) | hydrateRoot(<App />) |
HTML comes from server; React attaches JS logic to it. |
In CSR, the user sees a blank page until JavaScript loads.
In SSR + Hydration, the user sees HTML immediately, and React takes over afterward — giving both speed and interactivity.
🚀 Why Hydration is Important
- Faster initial load (better performance & SEO)
Users see content immediately from the server-rendered HTML. - Interactive components
Once hydrated, buttons, forms, and dynamic elements start working. - SEO-friendly pages
Search engines can crawl HTML before JS runs.
⚠️ Hydration Issues
Sometimes hydration can fail or mismatch between server and client renders, causing warnings like:
Warning: Text content did not match. Server: "Hello" Client: "Hi"
🔍 Causes:
- Conditional rendering differences between server and client
- Using
window,document, or browser-only APIs during SSR - Random values (like
Math.random()or timestamps) used during render
✅ Fix: Make sure the HTML rendered on the server exactly matches what React renders on the client before hydration.
🧱 Summary
| Concept | Description |
|---|---|
| Definition | Process of attaching React’s JS logic to pre-rendered HTML |
| Used in | SSR / SSG apps (like Next.js, Remix) |
| Function | hydrateRoot() (React 18+) |
| Goal | Make static HTML interactive |
| Common issues | Server-client markup mismatch |
In short:
React hydration is the process of turning server-rendered static HTML into a fully interactive React app on the client side by attaching event listeners and component logic.