Skip to main content
Renders a Preact virtual node into a DOM container element.

Signature

Parameters

ComponentChild
required
The virtual node to render. Can be a VNode, string, number, boolean, null, undefined, or arrays of these types.
ContainerNode
required
The DOM element to render into. This will become the parent container for the rendered content.

Return Value

Returns void. The function performs a side effect by rendering content into the DOM.

Description

The render function is the primary way to mount a Preact application to the DOM. It takes a virtual node tree and renders it into a specified DOM container. Key behaviors:
  • If parent is document, it automatically uses document.documentElement instead
  • Supports multiple calls on the same DOM node by tracking previous renders via _children property
  • Creates a root Fragment wrapper around the vnode
  • Handles both initial mounting and updates to existing trees

Usage Examples

Basic Rendering

Rendering Multiple Times

Rendering Text and Primitives

Implementation Details

The render function is implemented in src/render.js:12. When called:
  1. Checks if parent is document and uses documentElement instead
  2. Invokes options._root hook if present
  3. Wraps the vnode in a Fragment to create a consistent root
  4. Calls the internal diff algorithm to reconcile changes
  5. Commits all effects via commitRoot
  • hydrate - Attach Preact to existing server-rendered markup
  • h - Create virtual nodes
  • Fragment - Render multiple children without a wrapper element