Skip to main content
The @reatom/preact package provides Preact integration for Reatom with automatic dependency tracking, Preact Signals interoperability, and optimized components.

Installation

Optional: Install @preact/signals and @preact/signals-core for Preact Signals integration.

Setup Options

You have two setup options depending on your needs:

Option 1: Manual Setup

Wrap your application with the Reatom context provider:

Option 2: Automatic Tracking

Import the auto module to automatically wrap all components:
With automatic tracking, you can use atoms directly in components without any hooks or wrappers.

Core Components

reatomComponent

Create components with automatic atom dependency tracking:
Benefits:
  • Automatic subscription management
  • Fine-grained reactivity
  • No hooks needed
  • Suspense support

reatomFactoryComponent

Create components with initialization logic:

Preact Signals Integration

Reatom atoms can be converted to and from Preact Signals for seamless interoperability.

toPreact

Convert Reatom atoms to Preact signals:
Features:
  • Lazy subscription (only subscribes when signal is accessed)
  • Writable signals for writable atoms
  • Read-only signals for computed atoms
  • Cached (calling toPreact multiple times returns the same signal)

withPreact Extension

Add a .preact property to atoms:
Global Extension: You can apply this to all atoms automatically:

Form Binding

bindField

Bind form fields to Reatom atoms:
The bindField helper returns:
  • value or checked: Current field value
  • onChange: Change handler
  • onBlur: Blur handler
  • onFocus: Focus handler
  • error: Validation error message

Hooks

useFrame

Access the current Reatom frame:

useWrap

Create stable callbacks that execute in the Reatom context:

Complete Examples

Todo List with Auto Tracking

Using Preact Signals

TypeScript Support

Full TypeScript support with proper type inference:

Best Practices

1

Choose your setup

Use automatic tracking (@reatom/preact/auto) for simpler code, or manual setup for more control.
2

Use reatomComponent for atom-heavy components

Components that read many atoms benefit from automatic tracking and fine-grained updates.
3

Leverage Preact Signals when needed

Use toPreact for seamless integration with existing Preact Signals code.
4

Cache signal conversions

toPreact caches results, so it’s safe to call multiple times with the same atom.

Next Steps

  • Learn about Core Concepts for atoms and state management
  • Explore [Formshttps://github.com/reatom/reatom/tree/main/packages for advanced form handling
  • Check out [Asynchttps://github.com/reatom/reatom/tree/main/packages for asynchronous state