Blog>
Snippets

Batched Actions for Performance

Demonstrate the use of `batch()` API to dispatch multiple actions in a batch for reducing the number of renders and improving performance.
import { batch } from 'react-redux';

// Disptach multiple actions in a batch to update state
batch(() => {
  dispatch(actionOne());
  dispatch(actionTwo());
  dispatch(actionThree());
});
This code uses the 'batch' function from 'react-redux' to dispatch multiple actions together in a single batch. This ensures that React only re-renders once at the end of the batch, instead of re-rendering after each individual action, improving performance when dispatching multiple actions at once.