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: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
Debounced Search
Best Practices
Use actions for side effects
Use actions for side effects
Keep side effects in actions, not in computed values:
Name actions as verbs
Name actions as verbs
Action names should describe what they do:
Return meaningful values
Return meaningful values
Return values that indicate success or provide results:
Related Concepts
Atom
Store mutable state
Computed
Derive state automatically
Effects
Run reactive side effects
Extend
Customize action behavior