What is preact/compat?
preact/compat is a compatibility layer that allows you to use React libraries and components with Preact. It provides a React-compatible API surface, making it possible to leverage the vast React ecosystem while benefiting from Preact’s small size and performance.
preact/compat adds approximately 2-3kb to your bundle size, keeping Preact significantly smaller than React while maintaining compatibility.
Key Features
React API Compatible
Full support for React’s component API, hooks, and utilities
Ecosystem Access
Use popular React libraries like React Router, Redux, and more
Minimal Overhead
Only 2-3kb additional size compared to core Preact
Automatic Aliasing
Simple bundler configuration to alias React to Preact
Supported React Features
Thepreact/compat package provides comprehensive React compatibility. Based on the implementation in compat/src/index.js:157, here’s what’s included:
Core Components & APIs
All React Hooks
Advanced Features
Quick Setup
The easiest way to use preact/compat is to configure your bundler to alias React imports to Preact.Webpack Configuration
webpack.config.js
Vite Configuration
vite.config.js
Rollup Configuration
rollup.config.js
esbuild Configuration
build.js
React Version Compatibility
As seen in compat/src/index.js:43, preact/compat identifies itself as React 18.3.1 to ensure library compatibility.
- React 18.x features (concurrent features, automatic batching)
- React 17.x features (JSX transform, event delegation)
- React 16.x features (hooks, context, Suspense)
- Most React ecosystem libraries
Import Paths
preact/compat supports multiple import paths for React compatibility:Basic Usage Example
Once configured, you can use React code without any modifications:App.jsx
Next Steps
Migration Guide
Step-by-step guide to migrating from React to Preact
Differences
Learn about key differences between React and Preact