Skip to main content

Overview

Migrating from React to Preact with the compat layer is straightforward and can often be done without changing any application code. This guide walks you through the process.
Most React applications can be migrated to Preact by simply configuring bundler aliases and updating dependencies.

Migration Process

1

Install Preact

Remove React dependencies and install Preact:
Or keep React as a peer dependency if you’re migrating a library:
2

Update package.json

Update your package.json to reference Preact:
package.json
If you’re using TypeScript, you may also want to install @types/react and @types/react-dom as dev dependencies for better IDE support.
3

Configure Bundler Aliases

Configure your bundler to alias React imports to Preact. Choose your bundler:

Webpack

webpack.config.js

Vite

vite.config.js
The @preact/preset-vite plugin automatically configures aliases and optimizations for Preact.

Rollup

rollup.config.js

Parcel

Add to your package.json:
package.json

esbuild

build.js
4

Update TypeScript Configuration (if applicable)

If you’re using TypeScript, update your tsconfig.json:
tsconfig.json
The paths configuration helps TypeScript resolve the aliased imports correctly but doesn’t perform the actual aliasing. Your bundler configuration is still required.
5

Update Entry Point

If you’re using React 18’s createRoot API, no changes are needed. If you’re using the legacy ReactDOM.render, you can either:Option 1: Keep using the compat render function
index.jsx
Option 2: Switch to React 18 API (recommended)
index.jsx
Option 3: Use Preact’s render directly
index.jsx
6

Test Your Application

Build and run your application to ensure everything works:
Run your test suite:
Check your bundle size! You should see a significant reduction compared to React.
7

Fix Compatibility Issues (if any)

While most code works without changes, you may need to address:
  • Libraries that check for specific React internals
  • Code using deprecated lifecycle methods (though these are supported via compat/src/render.js:49)
  • Custom synthetic event handling
  • PropTypes validation (consider removing in production)
See the Differences page for details on specific edge cases.

Migrating Common Patterns

Class Components

Class components work without changes. The compat layer ensures full compatibility:
The PureComponent implementation in compat/src/PureComponent.js:14 performs shallow prop and state comparison just like React.

Hooks

All React hooks are supported:

Context API

Refs and forwardRef

Refs work identically to React. The forwardRef implementation in compat/src/forwardRef.js:12 ensures full compatibility:

Memo and Lazy

Portals

Portals are fully supported via compat/src/portals.js:70:

Handling Third-Party Libraries

Most Libraries Work Automatically

Popular libraries that work out of the box:
  • React Router
  • Redux / Redux Toolkit
  • React Query / TanStack Query
  • Zustand
  • React Hook Form
  • Formik
  • Styled Components
  • Emotion
  • Material-UI (MUI)
  • Chakra UI
  • Ant Design

Libraries That May Need Adjustments

Some libraries may require configuration:

Optimizing After Migration

Remove Compat for Core Code

Once migrated, consider importing from preact directly in your own code for smaller bundles:

Configure Aliases for Specific Libraries Only

You can be selective about what uses the compat layer:
webpack.config.js

Troubleshooting

Ensure your bundler aliases are configured correctly and that you’ve installed preact:
Double-check that your bundler configuration is being loaded.
Install React types as dev dependencies:
Update your tsconfig.json with the paths configuration shown in Step 4.
Some libraries perform deep checks on React internals. Options:
  1. Check if there’s a Preact-specific version of the library
  2. Report the issue to the library maintainers
  3. Look for alternative libraries
  4. Implement a compatibility shim
Update your test configuration to use the same aliases:
jest.config.js

Next Steps

Learn the Differences

Understand key differences between React and Preact

Compat Overview

Review all supported React features