Catalog / Preact Cheatsheet
Preact Cheatsheet
A concise reference for Preact, a fast 3kB alternative to React, with the same modern API. This cheat sheet covers the essentials for building performant web applications with Preact.
Core Concepts
Components
Components are the core building blocks of Preact applications. They manage state and render UI. Functional Components (Hooks):
Class Components:
|
JSX & Rendering
Preact uses JSX to describe UI. The JSX Example:
Rendering:
|
Virtual DOM
Preact uses a virtual DOM to efficiently update the actual DOM. It compares the previous and current virtual DOM trees and only applies the necessary changes. |
Hooks
useState
Declares a state variable.
|
|
Updating the state triggers a re-render.
|
Functional updates:
|
useEffect
Performs side effects in functional components.
|
The effect runs after every render. The dependency array controls when the effect runs. If the array is empty |
useRef
Creates a mutable ref object.
|
The ref object persists across renders. Useful for accessing DOM nodes or storing mutable values without causing re-renders. |
Accessing the current value:
|
|
Component Lifecycle
Lifecycle Methods (Class Components)
|
Preact CLI
Creating a New Project
Preact CLI is the recommended way to start a new Preact project.
|
Common Commands
|
Starts the development server with hot reloading. |
|
Builds the project for production. |
|
Serves the production build locally. |
Configuration
Preact CLI uses a
|