Signature
Parameters
The virtual node to render. Can be a VNode, string, number, boolean, null, undefined, or arrays of these types.
The DOM element to render into. This will become the parent container for the rendered content.
Return Value
Returnsvoid. The function performs a side effect by rendering content into the DOM.
Description
Therender 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
parentisdocument, it automatically usesdocument.documentElementinstead - Supports multiple calls on the same DOM node by tracking previous renders via
_childrenproperty - 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 insrc/render.js:12.
When called:
- Checks if parent is
documentand usesdocumentElementinstead - Invokes
options._roothook if present - Wraps the vnode in a Fragment to create a consistent root
- Calls the internal
diffalgorithm to reconcile changes - Commits all effects via
commitRoot