Group thunk dispatches
A thunk dispatches several actions and the resulting React updates should be rendered together.
Redux Toolkit · Function
batch groups multiple React updates into one render pass, commonly wrapping several dispatches made outside React. React 18 automatically batches updates, so explicit use is usually unnecessary there.
Call batch with a callback containing multiple dispatches when those dispatches occur outside React and should produce one combined render update. The function delegates batching to the underlying React renderer; with React 18, automatic batching generally makes explicit use unnecessary.
batch((fn: () => void))| Name | Type | Description |
|---|---|---|
fn | () => void | Callback containing the React updates or dispatches to batch. |
The page documents the callback signature but does not specify a separate return value.
batch.batch calls are generally unnecessary.import { batch } from 'react-redux'
import { increment } from './counterSlice'
export function incrementTwice() {
return (dispatch) => {
batch(() => {
dispatch(increment())
dispatch(increment())
})
}
}Groups the two dispatches so React performs one combined render update when batching is needed.
A thunk dispatches several actions and the resulting React updates should be rendered together.
A non-React callback performs multiple store updates in one event-loop turn.
A shared application supports React versions or environments where explicit renderer batching is still needed.
batch?batch accepts a callback: batch((fn: () => void)). Dispatches placed inside that callback can be combined into one React render update when they occur outside React.
Follow-up: Does it combine Redux subscriber notifications? No. It targets React rendering; Redux dispatches and selector work can still occur for each action.
batch?Use it around multiple dispatches in a thunk or another non-React execution path when avoiding multiple React renders matters. The callback is executed as the batching unit.
Follow-up: How does React 18 change this recommendation? React 18 automatically batches all state updates, so explicit use is usually unnecessary.
batch come from and how is renderer compatibility handled?The public function is exported from react-redux, and React-Redux chooses the correct renderer-specific batching implementation for ReactDOM and React Native. It is therefore used through the React-Redux package rather than imported directly from a renderer.
Follow-up: What should be checked before adding it to new code? Verify the application is not already relying on React 18 automatic batching.
unstable_batchedUpdatesReact 18 automatic batchingReactDOMReact Native