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:Advanced Patterns
Type-Safe Extensions
Conditional Extensions
Extension Best Practices
Keep extensions focused
Keep extensions focused
Each extension should do one thing well:
Return type-safe objects
Return type-safe objects
Use TypeScript to ensure type safety:
Don't change the atom reference
Don't change the atom reference
Extensions must return the same atom or additional methods:
Common Extension Recipes
Debounced Updates
Persistence
Related Concepts
Atom
Core state container
Actions
Extend actions with middleware
Computed
Extend derived state
Effects
Extend reactive effects