Overview
ThebatchUpdates method allows you to perform multiple state updates while only triggering subscriptions (effects and listeners) once after all updates are complete. This optimizes performance when you need to update multiple related state values.
Signature
Parameters
Function that contains all the state updates you want to batch.
- All state updates inside this function are batched
- Subscriptions are notified only after the callback completes
- If an error occurs, subscriptions are still triggered for successful updates
Return Value
This method does not return a value (void).Basic Example
Performance Optimization
Batching prevents unnecessary renders and computations:Async Operations
Handle data fetching with batched updates:Form Handling
Batch form field updates:Nested Batching
Batch updates can be nested (outer batch takes precedence):Error Handling
Updates before the error are still batched:Custom Actions Auto-Batch
Custom actions automatically batch their updates:Array Operations
Batch multiple array modifications:State Reconciliation
Sync multiple state values together:Reset Method Uses Batching
The built-inreset method automatically batches updates:
DOM Updates
Batch UI updates to prevent layout thrashing:Type Safety
TypeScript fully supports batchUpdates:When to Use Batching
UsebatchUpdates when:
- Updating multiple related state values
- Handling form submissions
- Processing API responses that affect multiple fields
- Performing bulk operations on arrays
- Resetting multiple values at once
- Synchronizing computed values with their dependencies
- Single state updates
- Updates that happen at different times
- When custom actions already batch for you
See Also
- createStore - Create a vanilla store
- effect - Subscribe to state changes
- getState - Access current state