Skip to main content

Overview

The extend() 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)

Extensions can either:
  • Return the target (with side effects like adding middleware)
  • Return an object with properties to assign to the target

Assigner Extension

Extensions that add properties or methods to the target.

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 functionality
  • withUndo() - Add undo/redo capability
  • withLocalStorage() - Persist to localStorage
  • withSessionStorage() - Persist to sessionStorage

Lifecycle

  • withInit() - Run initialization logic
  • withConnectHook() - Hook into subscribe/unsubscribe
  • withChangeHook() - Hook into state changes

Async

  • withAbort() - Add abort controller support
  • withAsync() - Enhanced async state handling
  • withAsyncData() - Async data fetching patterns
  • withSuspense() - React Suspense integration

Computed

  • withComputed() - Add computed properties
  • withMemo() - Add memoization

Debugging

  • withLogger() - Log state changes
  • withDevtools() - 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)