ScriptsApr 11, 2026·2 min read

Preact — Fast 3kB React Alternative with Same Modern API

Preact is a fast 3kB alternative to React with the same modern API. Virtual DOM, Components, Hooks — all in a tiny package. Drop-in compatible via preact/compat. Perfect for performance-critical apps and islands architecture.

TL;DR
Preact offers React-compatible components and hooks in a 3kB package with drop-in compatibility via preact/compat.
§01

What it is

Preact is a fast 3kB alternative to React that provides the same modern API: Virtual DOM, functional components, hooks, and context. It achieves this by focusing on the core rendering engine and dropping rarely-used features. For full React compatibility, preact/compat provides a drop-in layer that works with most React libraries.

Preact targets developers building performance-critical web applications where bundle size matters. It is ideal for widgets, embedded UIs, progressive web apps, and any project where loading speed is a priority.

§02

How it saves time or tokens

Preact's 3kB gzip size means faster page loads, especially on mobile networks and low-powered devices. The React-compatible API means you do not need to learn a new framework; existing React knowledge transfers directly. preact/compat lets you use React ecosystem libraries (React Router, React Query, etc.) without modifications. Migration from React to Preact is often a matter of aliasing imports.

§03

How to use

  1. Create a new Preact project:
npm init preact
  1. Or add Preact to an existing project:
npm install preact
  1. Write components using familiar React patterns:
import { render } from 'preact';
import { useState } from 'preact/hooks';

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}

render(<Counter />, document.getElementById('app'));
§04

Example

// Using preact/compat for React library compatibility
// vite.config.ts
import { defineConfig } from 'vite';

export default defineConfig({
  resolve: {
    alias: {
      'react': 'preact/compat',
      'react-dom': 'preact/compat',
      'react/jsx-runtime': 'preact/jsx-runtime'
    }
  }
});

// Now React libraries work with Preact
import { useQuery } from '@tanstack/react-query';
import { BrowserRouter, Route } from 'react-router-dom';
§05

Related on TokRepo

This tool integrates with standard development workflows and requires minimal configuration to get started. It is available as open-source software with documentation and community support through the official repository. The project follows semantic versioning for stable releases.

For teams evaluating this tool, the key advantage is reducing manual work in repetitive tasks. The automation provided by the built-in features means less custom code to maintain and fewer integration points to manage. This translates directly to lower maintenance costs and faster iteration cycles.

§06

Common pitfalls

  • Not all React libraries work with preact/compat; libraries that rely on React internals or concurrent mode features may break. Test compatibility before committing to a migration.
  • Preact's event system has minor differences from React (e.g., synthetic events); most code works unchanged, but edge cases in event handling may surface.
  • The 3kB size applies to Preact core; adding preact/compat increases the bundle, though it remains smaller than React.

Frequently Asked Questions

Can I use React libraries with Preact?+

Yes, via preact/compat. It provides a compatibility layer that aliases React imports to Preact equivalents. Most React libraries work without modification, including React Router, React Query, and Material UI.

How does Preact compare to React in performance?+

Preact is generally faster for initial page load due to its smaller bundle size (3kB vs ~40kB for React). Runtime rendering performance is comparable for most applications. The difference is most noticeable on slow networks and low-powered devices.

Can I migrate from React to Preact?+

Yes. For many projects, migration involves aliasing react and react-dom to preact/compat in your bundler configuration. No code changes are needed if your components use standard React APIs.

Does Preact support hooks?+

Yes. Preact supports useState, useEffect, useContext, useReducer, useMemo, useCallback, useRef, and all other standard hooks. Import them from preact/hooks.

Is Preact production-ready?+

Yes. Preact is used in production by companies of all sizes. It has been actively maintained since 2015 and has a stable API. The core team provides regular releases and security updates.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets