Preact

10.29.1 · active · verified Sun Apr 19

Preact is a lightweight, 3KB JavaScript library offering a React-compatible API for building user interfaces with a Virtual DOM. Currently in its stable v10.29.1 series, it maintains a rapid release cadence with frequent patch and minor updates, alongside public betas for upcoming major versions like 11.0.0-beta.1. Key differentiators include its extremely small footprint, which is beneficial for performance and bundle size, while largely replicating the modern React API including hooks, functional components, and class components. It offers extensive compatibility with the React ecosystem through its `preact/compat` alias, enabling many existing React libraries to function with minimal configuration. Preact features an optimized diffing algorithm, supports Server-Side Rendering (SSR), and includes developer tools and Hot Module Replacement (HMR) capabilities. It targets all modern browsers and maintains compatibility with IE11, providing a robust yet minimal alternative for web development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create and render a basic functional Preact component using JSX, including an initial mount and an in-place update to the DOM.

import { h, render } from 'preact';
// Configure JSX pragma for TypeScript or Babel via tsconfig.json or babel.config.js
// For TypeScript, add "jsxFactory": "h" to your compilerOptions.
// For Babel, configure @babel/plugin-transform-react-jsx with 'pragma': 'h'.
// Alternatively, add this pragma comment to the top of your JSX files:
/** @jsx h */

function Greeting({ name }) {
  return (
    <main>
      <h1>Hello, {name}!</h1>
      <p>This is a simple Preact application.</p>
      <button onClick={() => alert(`Welcome, ${name}!`)}>Say Hello</button>
    </main>
  );
}

// Initial render to the document body
render(
  <Greeting name="World" />,
  document.body
);

// Update the component in-place after 3 seconds
setTimeout(() => {
  render(
    <Greeting name="Preact User" />,
    document.body
  );
}, 3000);

view raw JSON →