The render function
Preact’srender() function mounts a component tree into a DOM element (src/render.js:12):
Basic usage
Importrender and mount your app:
Mounting to a container
It’s common to render into a dedicated container element:Initial render vs updates
Preact handles initial renders and subsequent updates differently:Initial render
On first render, Preact:- Creates a VNode tree from your components
- Generates real DOM nodes
- Inserts them into the parent container
- Calls lifecycle methods like
componentDidMount
Subsequent renders
When you callrender() again on the same container:
- Preact finds the previous VNode tree (stored in
parentDom._children) - Diffs the new tree against the old one
- Only updates the parts that changed
- Calls update lifecycle methods
Preact automatically detects if you’re rendering into a container that already has a Preact app and performs an efficient update.
Replacing content
When you render into a container, Preact replaces any existing content:<p>Loading...</p> is completely replaced by the rendered content.
Hydration
Hydration is the process of attaching Preact to server-rendered HTML instead of replacing it (src/render.js:66):
When to use hydrate
Usehydrate() when:
- You’re using server-side rendering (SSR)
- The DOM already contains the rendered HTML
- You want to attach event listeners and make it interactive
Hydration example
- Server-rendered HTML
- Client-side hydration
The component tree passed to
hydrate() must match the server-rendered HTML exactly, or Preact will log warnings.Rendering process
Here’s what happens when you callrender():
Step-by-step breakdown
- Prepare the VNode - The component is wrapped in a Fragment
- Find previous tree - Check if this container was rendered before
- Diff - Compare new and old VNode trees
- Update DOM - Apply minimal changes to real DOM
- Commit - Make all DOM changes visible at once
- Effects - Run refs, lifecycle callbacks, etc.
Real-world example
Here’s a complete example from Preact’s demo (demo/index.jsx:197):
- Imports
renderfrom Preact - Defines an
Appcomponent - Renders it into
document.body - Sets up client-side routing
Multiple render calls
You can callrender() multiple times on the same container to update the UI:
Each call to
render() on the same container performs an efficient diff and only updates what changed.Unmounting
To unmount a component tree, rendernull:
- Calls
componentWillUnmount()on all components - Removes all DOM nodes
- Cleans up event listeners and refs
Rendering to specific elements
You can render multiple independent Preact apps on the same page:Rendering fragments
You can render fragments directly:Performance considerations
Avoid rendering to document.body
Rendering directly todocument.body can interfere with browser extensions and dev tools:
Reuse containers
When updating, always render to the same container:Error boundaries
Wrap your render in a try-catch for error handling:For better error handling, consider using error boundary components that catch errors in their child components.