Skip to main content
Attaches event listeners and makes existing server-rendered markup interactive.

Signature

Parameters

ComponentChild
required
The virtual node tree that matches the existing server-rendered HTML structure.
ContainerNode
required
The DOM element containing the server-rendered content to hydrate.

Return Value

Returns void. The function performs a side effect by hydrating the existing DOM.

Description

The hydrate function is used to “hydrate” existing server-rendered HTML markup. Instead of creating new DOM nodes, it attaches event listeners and internal state to the existing DOM structure, making it interactive. This is particularly useful for:
  • Server-side rendering (SSR) scenarios
  • Improving initial page load performance
  • Progressive enhancement strategies

Implementation

The hydrate function is implemented in src/render.js:66 and works by:
  1. Setting the MODE_HYDRATE flag on the vnode
  2. Calling the render function with the hydration flag enabled
  3. Reusing existing DOM nodes instead of creating new ones

Usage Examples

Basic Hydration

Hydration with Data

Full SSR + Hydration Flow

Important Considerations

Markup Mismatch

The virtual node tree passed to hydrate must match the existing HTML structure. Mismatches can cause:
  • Unexpected behavior
  • Loss of hydration benefits
  • Console warnings in development

Event Handlers

Event handlers are not included in server-rendered HTML. The hydrate function attaches them during hydration:

Differences from render

  • render: Creates new DOM nodes, replacing existing content
  • hydrate: Reuses existing DOM nodes, only attaching handlers and state
  • render - Standard rendering without hydration
  • h - Create virtual nodes
  • preact-render-to-string - Server-side rendering utility