Skip to main content

Overview

The extend system is Reatom’s mechanism for adding capabilities to atoms and actions without modifying their core behavior. Extensions enable you to compose functionality in a modular, reusable way. Extensions can:
  • Add new methods to atoms/actions
  • Modify behavior with middleware
  • Transform parameters or return values
  • Add lifecycle hooks
  • Enable feature composition

Basic Usage

Extending Atoms

Extension Type Signature

Built-in Extension Utilities

withMiddleware

Add middleware that intercepts atom/action execution:

withTap

Observe state changes without modifying them:

withParams

Transform parameters before they reach the atom:

withComputed

Add computed capabilities to an atom:

Creating Custom Extensions

Extension that Returns the Target

Simplest form - modify the target and return it:

Extension with Additional Methods

Add multiple methods to an atom:

Extension with Middleware

Create extensions that modify behavior:

Extension with State Tracking

Action Extensions

withActionMiddleware

Extensions specifically for actions:

Extension Composition

Chain multiple extensions together:

Global Extensions

Apply extensions to all future atoms:
Global extensions affect all atoms/actions in your application. Use them sparingly and only for cross-cutting concerns like logging or analytics.

Advanced Patterns

Type-Safe Extensions

Conditional Extensions

Extension Best Practices

Each extension should do one thing well:
Use TypeScript to ensure type safety:
Extensions must return the same atom or additional methods:

Common Extension Recipes

Debounced Updates

Persistence

Atom

Core state container

Actions

Extend actions with middleware

Computed

Extend derived state

Effects

Extend reactive effects