Skip to main content
Components are the building blocks of Preact applications. They let you split your UI into independent, reusable pieces.

Component types

Preact supports two types of components:
Functional components are simple JavaScript functions that accept props and return JSX.
This is the recommended approach for most components. They are simpler, easier to test, and work seamlessly with hooks.

The Component class

Preact exports a BaseComponent class (aliased as Component) that provides core functionality for class-based components. Here’s how it’s defined in src/component.js:19:
The Component class provides two key methods:

setState()

Updates component state and schedules a re-render:

forceUpdate()

Immediately triggers a re-render, bypassing shouldComponentUpdate():

Props

Props (short for “properties”) are arguments passed to components. They are read-only and flow down from parent to child.

Props in class components

In class components, props are available via this.props:

State

State is private data managed within a component. When state changes, the component re-renders.

State in class components

Here’s a real example from Preact’s demo code (demo/todo.jsx:5):
The render() method can destructure both props and state from its arguments for cleaner code.

Updating state

You can pass an object or a function to setState():
Never modify state directly. Always use setState() to ensure the component re-renders correctly.

Functional vs class components

Here’s the same component implemented both ways:

Real-world example

Here’s a complete example from Preact’s demo showing both component types working together (demo/context.jsx:5):
This example demonstrates prop passing, state management, lifecycle methods, and component composition.

Component render queue

When you call setState() or forceUpdate(), Preact doesn’t immediately re-render the component. Instead, it adds the component to a render queue (src/component.js:185):
This batching mechanism ensures efficient rendering by processing multiple updates together.