Batch Update in React
Published: 2022-09-01 05:24:39

## What is batch update?
Batching in React when React groups the multiple change is state are grouped into single update for better performance.

Every time we change the state React renders the components of the page.  
Here in the given example we are changing the state 3 times but the render will happen only once.

```
import React from "react";
class App extends React.Component {

    constructor(props) {
        super(props);

        this.state = {
            name: '',
            likes: '',
            product: ''
        };
    }
    handleClick = () => {
        this.setState({
            name:"john",
        })
        this.setState({
            likes:"cricket",
        })
        this.setState({
            product:"bat"
        })

    }

    render() {
        console.log("rendering");

        return (
            <div className="App">
                <div>
                    <button onClick={this.handleClick}>Next</button>
                </div>
            </div>
        );
    }
}
export default App;
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltxo24tpxr9909iuyls8.png)

What happens here is when we press the button the state is changed using this.setSate and react batches the all of these changes and renders it one go.

In earlier versions of React, batching was only done for the event handlers.

React v18 ensures that state updates made from any location are batched by default. This will batch state updates, including native event handlers, asynchronous operations, timeouts, and intervals.

Here in this example if we for React v17 React will change the state of the components we by on and hence when we `console.log(this.state)` see the **React 17** : `{ counter1: 1, counter2: 0 }` because the state has been and rendered for the the first state change.


```
import React from "react";
class App extends React.Component {
    state = {
        counter1: 0,
        counter2: 0
    }

    handleClick = () => {
        setTimeout(() => {
            this.setState(({ counter1 }) => ({ counter1: counter1 + 1 }));
            console.log(this.state);
            //React 17 : { counter1: 1, counter2: 0 }
            //React 18 with createRoot : { counter1: 0, counter2: 0 } 
            this.setState(({ counter2 }) => ({ counter2: counter2 + 1 }));
        });
    };

    render() {
        console.log('Rerendered');
        console.log(this.state);
        // {counter1: 1, counter2: 1}
        return (
            <div className='App'>
                <button onClick={this.handleClick}>Click me</button>
            </div>
        )
    }
}

export default App;

```
In react v18
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6hx8ul6u8yp374ls1tu.png)



## References
* https://reactjs.org/blog/2022/03/08/react-18-upgrade-guide.html

* https://blog.logrocket.com/simplifying-state-management-in-react-apps-with-batched-updates/

* https://blog.bitsrc.io/automatic-batching-in-react-18-what-you-should-know-d50141dc096e#:~:text=React%20uses%20batching%20to%20group,support%20batching%20for%20browser%20events.

* https://blog.saeloun.com/2021/07/22/react-automatic-batching.html