How does React Batch Multiple State Updates?

Introduction

React batches multiple state updates to improve performance by minimizing the number of re-renders. It does this through an asynchronous process that groups state updates together and performs them in a single render cycle. Let's explore this process in detail with some code examples:

Asynchronous Nature of setState()

In React, when you call setState(), React doesn't immediately apply the state changes. Instead, it schedules the updates to be processed in batches. This means that multiple calls to setState() within the same synchronous event handler or lifecycle method do not trigger immediate re-renders. Here's an example:

class Counter extends React.Component {
  state = {
    count: 0,
  };

  handleClick = () => {
    this.setState({ count: this.state.count + 1 });
    this.setState({ count: this.state.count + 1 });
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.handleClick}>Increment</button>
      </div>
    );
  }
}

In this example, when the button is clicked, handleClick() is called, which updates the state three times consecutively. However, React batches these state updates and performs a single re-render.

Event Loop and Optimization

React leverages the browser's event loop to batch state updates. When you call setState() multiple times within the same event loop iteration (such as within a single event handler execution), React will batch these updates together. Additionally, React intelligently merges multiple state updates into a single update operation whenever possible to optimize performance.

Explicit Batching with batchedUpdates()

While React handles batching automatically in most cases, you can also manually batch updates using the batchedUpdates() API. This can be useful in scenarios where you need to ensure that a series of state updates are processed together. Here's how you can use batchedUpdates():

import React from 'react';
import ReactDOM from 'react-dom';

class MyComponent extends React.Component {
  handleClick = () => {
    ReactDOM.unstable_batchedUpdates(() => {
      this.setState({ count: this.state.count + 1 });
      this.setState({ count: this.state.count + 1 });
      this.setState({ count: this.state.count + 1 });
    });
  };

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.handleClick}>Increment</button>
      </div>
    );
  }
}

In this example, setState() calls within batchedUpdates() are grouped together and processed in a single render cycle, even though they're called within the same event handler.

Summary

React's batching mechanism for state updates helps optimize performance by minimizing the number of re-renders. It does this through an asynchronous process that groups state updates together and performs them in a single render cycle, leveraging the browser's event loop. Additionally, React intelligently merges multiple state updates into a single update operation whenever possible