Skip to main content
You can install Preact using your preferred package manager or include it directly via CDN.

Package manager installation

Install Preact using npm, yarn, or pnpm:

Core packages

Preact provides several packages for different use cases:

Main package

The core Preact library includes everything you need to build components:
This gives you access to:
  • render and hydrate for mounting components
  • createElement (aliased as h) for creating virtual DOM nodes
  • Component class for building class components
  • Fragment for grouping elements without a wrapper
  • createContext for context API
  • createRef for accessing DOM elements

Hooks

For functional components with state and effects, install the hooks package:
Preact hooks are available from preact/hooks:
Available hooks:
  • useState - Add state to functional components
  • useEffect - Perform side effects
  • useLayoutEffect - Synchronous version of useEffect
  • useReducer - Alternative to useState for complex state
  • useRef - Create mutable refs
  • useMemo - Memoize expensive computations
  • useCallback - Memoize callback functions
  • useContext - Access context values
  • useImperativeHandle - Customize ref exposure
  • useDebugValue - Display custom labels in DevTools
  • useErrorBoundary - Handle errors in components
  • useId - Generate unique IDs

React compatibility layer

For using React libraries with Preact, install the compatibility layer:
Then alias react and react-dom to preact/compat in your bundler configuration:

CDN installation

You can use Preact directly in the browser without a build step using a CDN:

Package.json setup

After installation, your package.json should include Preact:
package.json

JSX configuration

To use JSX syntax, you need to configure your build tool. Preact uses the h function (or createElement) for JSX transformation.

Babel configuration

Create a .babelrc or babel.config.js file:

TypeScript configuration

For TypeScript projects, configure tsconfig.json:
tsconfig.json
Alternatively, use the classic JSX transform:
tsconfig.json

Verifying installation

Create a simple test file to verify your installation:
index.js

Import paths

Preact exports are organized by functionality:

Next steps

Quick start guide

Build your first Preact application with our step-by-step guide