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
Create your first component
Create an Note: To use JSX syntax, you’ll need a build tool. For now, you can use the
index.js file with a simple component:index.js
h function directly:index.js
Add state with hooks
Make your component interactive by adding state with the Without JSX:
useState hook:index.js
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: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