Overview
TheCustomActions and CustomActionsBuilder types enable you to define custom state update logic beyond the default setter actions. This is useful for complex state updates, computed actions, or business logic.
CustomActions
void.
Characteristics
- Flexible Signature: Actions can accept any number of arguments of any type
- Void Return: Actions modify state but don’t return values
- Record Structure: Simple key-value mapping of action names to functions
CustomActionsBuilder
Type Parameters
TState: The state object typeTCustomActions: The custom actions object type extendingCustomActions
Parameters
Receives a singlebuilderParams object with the following properties:
CustomActionsBuilderParams
getState
actions
setCount, setUsername). Use these to update individual state properties.
reset
keys: Variable number of state keys to reset
Usage Examples
Basic Custom Actions
Custom Actions with Parameters
Complex Business Logic
Async Custom Actions
Computed Actions
Type Inference
TypeScript automatically infers types for custom actions:Utility Types
RemoveReadonly
Actions
set{PropertyName} and accepts either a new value or an updater function.
ActionKey
set.
Best Practices
- Type Safety: Always define explicit interfaces for custom actions
- Naming: Use descriptive action names that reflect their purpose
- Single Responsibility: Each action should do one thing well
- Async Handling: Properly handle errors in async actions
- State Updates: Use the provided
actionsobject instead of direct mutation - Reset Logic: Use the
resetfunction for resetting to initial values - Performance: Batch related state updates in a single action
Related
- create() - Main function that accepts custom actions builder
- Actions Guide - Learn more about custom actions
- TypeScript Guide - TypeScript best practices