Overview
Theaction() function creates a container for complex logic, side effects, and orchestration of multiple state updates. Unlike atoms, actions are meant to be called with parameters and can return values. Actions have atom-like features (subscribe, extend) and track their call history.
Import
Signature
Parameters
(...params: Params) => Payload
required
The function containing the action’s logic. It can accept any parameters and return any value. The function can be synchronous or asynchronous.
string
Optional name for the action. Useful for debugging and dev tools. If not provided, an auto-generated name based on the function name will be used.
Returns
Action<Params, Payload>
An action instance with the following interface:
Examples
Basic Action
State Updates in Actions
Async Actions
Subscribing to Actions
No Arguments Action
Complex Action Logic
Actions with Error Handling
Action Middleware
Type Information
Action Interface
ActionState Interface
GenericAction Type
Key Characteristics
- Non-Reactive: Actions are not reactive atoms - they don’t track dependencies
- Call History: Actions maintain a state array of all calls with their parameters and payloads
- Auto-Cleanup: The action state is automatically cleared after each transaction
- Subscribable: Actions can be subscribed to track when they’re called
- Extensible: Actions support the same extension system as atoms
Related
- atom() - Create mutable state containers
- computed() - Create derived state
- effect() - Create reactive side effects
- extend() - Add functionality to actions
- withActionMiddleware() - Add middleware to actions