Overview
Theextend() method is the core extension mechanism in Reatom that allows adding functionality to atoms and actions. Extensions can add properties, methods, or modify behavior while maintaining the original reference identity of the atom/action.
Import
Signature
Parameters
Ext[]
required
One or more extension functions to apply. Each extension receives the atom/action and either returns the same instance with modified behavior, or returns an object with additional properties to be assigned.
Returns
Merge<This, Extensions>
The original atom/action with all extensions applied. The reference identity is preserved - the same object is returned with modifications applied.
Extension Types
Generic Extension (Ext)
- Return the target (with side effects like adding middleware)
- Return an object with properties to assign to the target
Assigner Extension
Examples
Basic Extension
Multiple Extensions
Creating Custom Extensions
Extension with Parameters
Middleware Extensions
Extending Actions
Chaining Extensions
Extension Type Safety
Extension Limits
Built-in Extensions
Reatom provides many built-in extensions:State Management
withReset()- Add reset functionalitywithUndo()- Add undo/redo capabilitywithLocalStorage()- Persist to localStoragewithSessionStorage()- Persist to sessionStorage
Lifecycle
withInit()- Run initialization logicwithConnectHook()- Hook into subscribe/unsubscribewithChangeHook()- Hook into state changes
Async
withAbort()- Add abort controller supportwithAsync()- Enhanced async state handlingwithAsyncData()- Async data fetching patternswithSuspense()- React Suspense integration
Computed
withComputed()- Add computed propertieswithMemo()- Add memoization
Debugging
withLogger()- Log state changeswithDevtools()- Redux DevTools integration
Type Information
Extension Types
Merge Type
Key Characteristics
- Reference Identity: Extensions modify the original object - they don’t create a copy
- Property Assignment: Extensions can add new properties and methods
- Middleware Support: Extensions can intercept and modify behavior
- Type Safety: Full TypeScript support with type inference
- Composable: Extensions can be chained together
- No Override: Extensions cannot override existing properties (will throw an error)
Related
- atom() - Create atoms that can be extended
- action() - Create actions that can be extended
- computed() - Create computed atoms that can be extended
- withMiddleware() - Create middleware extensions
- Global Extensions - Apply extensions to all atoms