Skip to main content

Overview

Actions are Reatom’s way to encapsulate complex logic, perform side effects, and orchestrate multiple state updates. Unlike atoms which store state, actions represent operations that can be executed with parameters and return values. Actions:
  • Accept parameters when called
  • Can perform side effects (API calls, logging, etc.)
  • Can update multiple atoms
  • Return values like regular functions
  • Track their call history
  • Have atom-like features (subscribe, extend)

Creating Actions

Basic Usage

Create an action with a function:

Type Signature

Actions with Return Values

Actions can return values just like regular functions:

Async Actions

Actions work seamlessly with async operations:
Use the wrap utility for abortable async operations within actions. See the Effects documentation for more details.

Subscribing to Actions

Actions track their call history and can be subscribed to:
Action call history is automatically cleared after each transaction cycle. This prevents memory leaks.

Orchestrating State Updates

Actions excel at coordinating updates across multiple atoms:

Action Composition

Actions can call other actions:

Reading Atoms in Actions

Actions can read atom values to make decisions:

Action Middleware

Extend actions with middleware to add custom behavior:

Common Patterns

Form Submission

Optimistic Updates

Best Practices

Keep side effects in actions, not in computed values:
Action names should describe what they do:
Return values that indicate success or provide results:

Atom

Store mutable state

Computed

Derive state automatically

Effects

Run reactive side effects

Extend

Customize action behavior