Compat API Overview
Thepreact/compat package provides a React compatibility layer that allows you to use React components and libraries with Preact. It’s a drop-in replacement for React that implements the React API on top of Preact.
Installation
Import Patterns
Aliasing React to Preact
The most common way to use compat is to aliasreact and react-dom to preact/compat:
Direct Import
You can also import directly frompreact/compat:
Exported APIs
Core Components
- Component - Base component class
- PureComponent - Component with shallow prop/state comparison
- Fragment - Wrapper component that renders children without a DOM node
- StrictMode - Development mode helper (passthrough in Preact)
Element Creation
- createElement - Create a virtual DOM node
- createFactory - Legacy factory function for creating elements
- cloneElement - Clone and return a new element
- isValidElement - Check if an object is a valid element
- isFragment - Check if an element is a Fragment
- isMemo - Check if a component is memoized
Refs
- createRef - Create a ref object
- forwardRef - Forward refs to child components
Context
- createContext - Create a Context object
Higher-Order Components
- memo - Memoize functional components
- forwardRef - Pass refs through components
Hooks
All React hooks are available:- useState - State management
- useEffect - Side effects
- useLayoutEffect - Synchronous side effects
- useContext - Access context values
- useReducer - Complex state management
- useCallback - Memoize callbacks
- useMemo - Memoize computed values
- useRef - Mutable ref objects
- useImperativeHandle - Customize ref values
- useDebugValue - Debug custom hooks
- useId - Generate unique IDs
React 18 Hooks
- useInsertionEffect - Insert styles before layout effects
- useTransition - Mark updates as transitions
- useDeferredValue - Defer value updates
- useSyncExternalStore - Subscribe to external stores
- startTransition - Mark updates as non-urgent
Suspense
- Suspense - Suspend rendering while loading async data
- lazy - Lazy load components
Utilities
- Children - Utilities for manipulating children
- createPortal - Render children into a different DOM node
- render - Render a component into a container
- hydrate - Hydrate server-rendered markup
- unmountComponentAtNode - Remove a mounted component
- findDOMNode - Get the DOM node for a component
- flushSync - Flush updates synchronously (no-op in Preact)
- unstable_batchedUpdates - Batch state updates (no-op in Preact)
Version
Preact’s compat layer reports itself as React version18.3.1 for maximum compatibility with third-party libraries.
Type Definitions
Full TypeScript definitions are available inpreact/compat/src/index.d.ts, including:
- Component types and interfaces
- Event types
- HTML and SVG attribute types
- Hook types
- Utility types
Differences from React
Whilepreact/compat aims for maximum compatibility, there are some minor differences:
- flushSync - Implemented as a passthrough function
- unstable_batchedUpdates - Implemented as a passthrough function
- StrictMode - Renders children without additional checks
- Preact is faster and smaller than React
Source Files
The compat implementation can be found in:- compat/src/index.js:1-237 - Main exports
- compat/src/index.d.ts:1-433 - TypeScript definitions