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:renderandhydratefor mounting componentscreateElement(aliased ash) for creating virtual DOM nodesComponentclass for building class componentsFragmentfor grouping elements without a wrappercreateContextfor context APIcreateReffor accessing DOM elements
Hooks
For functional components with state and effects, install the hooks package:preact/hooks:
useState- Add state to functional componentsuseEffect- Perform side effectsuseLayoutEffect- Synchronous version of useEffectuseReducer- Alternative to useState for complex stateuseRef- Create mutable refsuseMemo- Memoize expensive computationsuseCallback- Memoize callback functionsuseContext- Access context valuesuseImperativeHandle- Customize ref exposureuseDebugValue- Display custom labels in DevToolsuseErrorBoundary- Handle errors in componentsuseId- Generate unique IDs
React compatibility layer
For using React libraries with Preact, install the compatibility layer: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, yourpackage.json should include Preact:
package.json
JSX configuration
To use JSX syntax, you need to configure your build tool. Preact uses theh function (or createElement) for JSX transformation.
Babel configuration
Create a.babelrc or babel.config.js file:
TypeScript configuration
For TypeScript projects, configuretsconfig.json:
tsconfig.json
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