Preact
Overview
Preact is a fast, ~4kB alternative to React with the same modern API (components, virtual DOM, hooks). It targets bundle-size-sensitive projects and can often run existing React code via the preact/compat aliasing layer. Read this when you need React-like ergonomics with a tiny footprint or are migrating between React and Preact.
Official sources
- Docs: https://preactjs.com/guide/v10/getting-started/
- Repo: https://github.com/preactjs/preact
- Install / download: https://preactjs.com/guide/v10/getting-started/
Install / setup
Scaffold a project with the official create-preact initializer (copied verbatim from preactjs.com/guide/v10/getting-started/):
npm init preact
To add Preact to an existing build:
npm install preact
Core concepts
- Components & virtual DOM — same component model and diffing approach as React.
- Hooks —
useState,useEffect, etc. viapreact/hooks. - JSX /
h— JSX compiles to Preact'sh(hyperscript) factory; configurable per build. preact/compat— alias layer mappingreact/react-domto Preact so many React libraries work unchanged.- Signals —
@preact/signalsoffers fine-grained reactive state as an alternative to hooks. - Small footprint — the runtime is intentionally tiny, reducing load and parse time.
Best practices
- Import hooks from
preact/hooks(not from areactimport) unless usingpreact/compat. - Use
preact/compataliases in your bundler when consuming React-ecosystem libraries. - Consider
@preact/signalsfor performant shared state in larger apps. - Pin the Preact major version and verify third-party React libs against
compat.
Common pitfalls
- Assuming 100% React API parity → some React internals/behaviors differ; test libraries under
compat. - Mixing real
reactand Preact in one bundle without aliasing → configure the bundler alias so only one runtime loads. - Forgetting JSX pragma config when not using a preset → set the JSX import source /
hpragma per the docs.
Examples
import { render } from 'preact';
import { useState } from 'preact/hooks';
function Counter() {
const [count, setCount] = useState(0);
return <button => setCount(count + 1)}>Clicked {count} times</button>;
}
render(<Counter />, document.getElementById('app'));
Further reading
- https://preactjs.com/guide/v10/getting-started/ — getting started guide
- https://preactjs.com/guide/v10/switching-to-preact/ — switching from React via compat
Related skills
- ../react — the API Preact mirrors
- ../solidjs — fine-grained reactive alternative