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
Install Preact
Remove React dependencies and install Preact:Or keep React as a peer dependency if you’re migrating a library:
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.Configure Bundler Aliases
Update TypeScript Configuration (if applicable)
If you’re using TypeScript, update your
tsconfig.json:tsconfig.json
Update Entry Point
If you’re using React 18’s Option 2: Switch to React 18 API (recommended)Option 3: Use Preact’s render directly
createRoot API, no changes are needed. If you’re using the legacy ReactDOM.render, you can either:Option 1: Keep using the compat render functionindex.jsx
index.jsx
index.jsx
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.
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)
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. TheforwardRef 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 frompreact 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
Build errors about React not found
Build errors about React not found
Ensure your bundler aliases are configured correctly and that you’ve installed preact:Double-check that your bundler configuration is being loaded.
TypeScript errors about React types
TypeScript errors about React types
Install React types as dev dependencies:Update your
tsconfig.json with the paths configuration shown in Step 4.Third-party library doesn't work
Third-party library doesn't work
Some libraries perform deep checks on React internals. Options:
- Check if there’s a Preact-specific version of the library
- Report the issue to the library maintainers
- Look for alternative libraries
- Implement a compatibility shim
Tests are failing
Tests are failing
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