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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 70e5a9d3-35a3-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
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.
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.
How to use
- Create a new Preact project:
npm init preact
- Or add Preact to an existing project:
npm install preact
- 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'));
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';
Related on TokRepo
- AI Tools for Coding — Developer tools and frameworks
- Featured Workflows — Top-rated development tools
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.
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/compatincreases the bundle, though it remains smaller than React.
常见问题
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.
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.
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.
Yes. Preact supports useState, useEffect, useContext, useReducer, useMemo, useCallback, useRef, and all other standard hooks. Import them from preact/hooks.
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.
引用来源 (3)
- Preact Official Site— Preact is a fast 3kB alternative to React with the same API
- Preact GitHub— preact/compat provides drop-in React compatibility
- Preact Documentation— Preact supports hooks, context, and functional components
讨论
相关资产
Polars — Blazingly Fast DataFrame Library in Rust
Polars is an extremely fast DataFrame library written in Rust with bindings for Python, Node.js, and R. Uses Apache Arrow columnar format, lazy evaluation, and multi-threaded query execution. The modern alternative to pandas for data engineering and analytics.
Gatsby — React-Based Framework for Performant Static Sites
Gatsby is a React-based open-source framework for building fast, secure websites and apps. It combines static site generation with dynamic capabilities, pulling data from any source via GraphQL.
Wouter — Minimalist 2KB Router for React and Preact
A tiny (~2.2KB) routing library for React and Preact that provides hooks-first API with nested routes, pattern matching, and SSR support without the weight of larger routers.
GitUI — Blazing-Fast Terminal UI for Git Written in Rust
GitUI brings a Rust-fast terminal interface to Git: browse commits, stage hunks, view diffs, manage branches, and resolve conflicts — all without leaving the keyboard. A great complement (or alternative) to lazygit.