Skip to main content
This guide will walk you through creating your first Preact application step by step. By the end, you’ll have a working interactive component that demonstrates Preact’s core features.

Prerequisites

Before you begin, make sure you have:
  • Node.js 20.0 or later installed
  • A package manager (npm, yarn, or pnpm)
  • A text editor or IDE
  • Basic knowledge of JavaScript and HTML

Create your first app

1

Set up your project

Create a new directory and initialize a package.json file:
Install Preact:
2

Create an HTML file

Create an index.html file as your entry point:
index.html
3

Create your first component

Create an index.js file with a simple component:
index.js
Note: To use JSX syntax, you’ll need a build tool. For now, you can use the h function directly:
index.js
4

Add state with hooks

Make your component interactive by adding state with the useState hook:
index.js
Without JSX:
index.js
5

Add effects

Use the useEffect hook to perform side effects:
index.js
6

Build with components

Break your UI into reusable components:
index.js

Working with class components

While functional components with hooks are recommended, Preact also supports class components:

Component lifecycle

Class components have lifecycle methods for different stages:
With functional components, use hooks instead:

Complete example

Here’s a complete todo app that demonstrates multiple concepts:

Key concepts recap

Render function

Use render() to mount components to the DOM. It accepts a component and a parent DOM element.

useState hook

Add state to functional components. Returns current state and a setter function.

useEffect hook

Perform side effects like data fetching, subscriptions, or DOM manipulation.

Component composition

Break your UI into small, reusable components that accept props and return JSX.

Next steps

Now that you’ve built your first Preact app, explore more features:

Learn about hooks

Dive deeper into hooks for state management and side effects

Explore the API

Browse the complete Preact API documentation

Server-side rendering

Learn how to render Preact components on the server

React compatibility

Use React libraries with preact/compat