Skip to main content

hydrateRoot

Hydrates a React application in a browser DOM container that was previously rendered by a React server API.

Reference

hydrateRoot(container, reactNode, options?)

Call hydrateRoot to attach React to existing HTML that was rendered by React in a server environment.
/src/client/ReactDOMRoot.js:274-358

Parameters

Document | Element
required
The DOM element that contains the server-rendered HTML. Must match the container used on the server.Validation:
  • Must be a valid DOM element or Document
  • Should contain server-rendered React HTML
  • Cannot be a DocumentFragment (unlike createRoot)
ReactNode
required
The React node that was used to render the existing HTML on the server. This must be identical to what was rendered on the server.
The reactNode must be identical to what was rendered on the server. Differences will cause hydration warnings or errors.
HydrateRootOptions
Optional configuration object for hydration.

Returns

RootType
A root object with render, unmount, and unstable_scheduleHydration methods.

root.render(reactNode)

Updates the hydrated root after initial hydration. Works the same as createRoot().render().

root.unmount()

Unmounts the React tree and cleans up. Same behavior as createRoot().unmount().

root.unstable_scheduleHydration(target)

Schedules eager hydration of a specific DOM node.
This is an unstable API and may change in future versions.

Usage Examples

Basic SSR Hydration

Server (Node.js):
Client:

With Error Tracking

With Suspense Boundaries

Progressive Hydration

Handling Hydration Mismatches

Hydration mismatches occur when the server-rendered HTML doesn’t match the client-rendered React tree.

Common Causes

  1. Incorrect nesting - Invalid HTML structure
  2. Browser extensions - Modify the DOM
  3. Date/time - Server and client timestamps differ
  4. Random values - Using Math.random() or Date.now()
  5. Environment differences - window/document available only on client

Example: Date/Time Mismatch

Problem:
Solution 1: Suppress hydration warning
Solution 2: Use effect to update on client
Solution 3: Two-pass rendering

Debugging Hydration Errors

Enable detailed hydration logging in development:
React will log:
  • Which component caused the mismatch
  • What the server rendered
  • What the client expected
  • The component stack trace

TypeScript

Migration from React 17

Before (React 17)

After (React 18+)

Performance Optimization

Selective Hydration

React 18 automatically prioritizes hydration based on user interactions:
If user clicks on Sidebar before it hydrates, React will prioritize Sidebar hydration.

Streaming SSR + Selective Hydration

Server:
Client:
Benefits:
  • HTML streams to browser before all data is ready
  • Page becomes interactive sooner
  • React hydrates high-priority content first

Common Pitfalls

Don’t modify DOM before hydration

Ensure server/client parity

Browser Compatibility

Same requirements as createRoot:
  • Modern browsers (Chrome 90+, Firefox 88+, Safari 14.1+, Edge 90+)
  • ES6 features: Map, Set, Promise
  • DOM features: Element, Document