Blog>
Snippets

Batching Updates in TanStack Store for Performance

Provide an example of batching multiple state updates in TanStack Store into a single re-render for performance optimization.
import { batch } from 'tanstack-store';

// Assume we have state setters for 'count' and 'isLoggedIn'
function updateMultipleStates() {
  batch(() => {
    // Update the count state
    setCount((prevCount) => prevCount + 1);
    // Update the isLoggedIn state
    setIsLoggedIn(true);
  });
  // This batch function ensures both states are updated in a single re-render
}
This code snippet demonstrates how to batch multiple state updates in TanStack Store, thereby minimizing re-renders by consolidating them into a single update cycle. It updates both 'count' and 'isLoggedIn' states within a single batched operation.