What is Hydration?
TL;DR
Process where React attaches event handlers and state to server-rendered HTML, making it interactive.
Hydration is the process where React "activates" the static HTML received from the server by attaching event handlers, initializing state, and making the page fully interactive. This bridges the gap between server-rendered content and client-side interactivity.
How Hydration Works:
- Server sends fully-rendered HTML to browser
- User sees content immediately (fast first paint)
- React JavaScript bundle loads in background
- React hydrates the HTML by attaching interactivity
- Page becomes fully interactive
Example in Frontend Accelerator:
'use client'; // Client Component that needs hydration
import { useState } from 'react';
export function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>
Count: {count}
</button>
);
}
Key Concepts:
- Hydration mismatch: When server HTML differs from client render, React warns you
- Selective hydration: React 18 can prioritize hydrating interactive parts first
- Progressive hydration: Components hydrate as they're needed
- Suspense boundaries: Control hydration timing with React Suspense
Avoiding Hydration Errors:
- Don't use browser-only APIs during server render
- Ensure server and client render the same content
- Use suppressHydrationWarning for dynamic content like dates
AI-Friendly: The 'use client' directive explicitly marks components that require hydration. AI tools understand which code runs on the server vs client, can identify potential hydration mismatches, and suggest appropriate fixes when server and client rendering differ.