# Hydration

> The Hydration is the process of attaching JS event handlers and state to HTML rendered on the server.

## What is hydration and why do I get hydration mismatch errors?

The process in which a client-side framework takes over server-rendered HTML, attaches event listeners and restores state. Until hydration is complete, the page is visible but not interactive.

- Topic: Web Application
- Type: process

## Example

Wrong:

```jsx
function Clock() {
  return <span>{new Date().toLocaleTimeString()}</span>;
}
```

Right:

```jsx
function Clock() {
  const [time, setTime] = useState(null);
  useEffect(() => setTime(new Date().toLocaleTimeString()), []);
  return <span>{time ?? '--:--'}</span>;
}
```

The server and the first client render both show a placeholder, and the real time is set after hydration, so the markup matches.

## Common mistakes

- Rendering a locale date on the server and client, which gives different text.
- Reading `localStorage` during render, which does not exist on the server.

## Key points

- Server and client render the same markup, with no `Date.now()`, `Math.random()` or `window` checks during render.
- Browser-only code runs in effects or event handlers, not during render.
- Hydration warnings are fixed at the source, not hidden with suppress flags.

## Interview questions

### Why is hydration slow?

The browser must download, parse and run the JS for every hydrated component before it responds to input. Less client JS, islands or server components reduce the cost.

### When is it OK to suppress a hydration warning?

Only for one element whose text must differ, like a timestamp. `suppressHydrationWarning` works one level deep and does not fix mismatches in children.

### What is the difference between hydration and rendering?

Rendering creates DOM from scratch, while hydration reuses the DOM the server already sent and attaches event handlers and state to it.

## Related cards

- [SSR](https://wonderium.dev/web/ssr.md)
- [hydrateRoot()](https://wonderium.dev/react/hydrate-root.md)
- [Islands Architecture](https://wonderium.dev/web/islands-architecture.md)
- [Streaming SSR](https://wonderium.dev/web/streaming-ssr.md)
- [Server Components](https://wonderium.dev/react/server-components.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/hydration
- Last updated: 2026-11-25
