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
Returnsvoid. The function performs a side effect by hydrating the existing DOM.
Description
Thehydrate 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 insrc/render.js:66 and works by:
- Setting the
MODE_HYDRATEflag on the vnode - Calling the
renderfunction with the hydration flag enabled - 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 tohydrate 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. Thehydrate function attaches them during hydration:
Differences from render
render: Creates new DOM nodes, replacing existing contenthydrate: Reuses existing DOM nodes, only attaching handlers and state