What’s Automatic Batching? React 18 feature explained

If you’ve ever built a component in React, chances are you’ve used state variables. If you’ve ever build a kinda-complex component in React, chances are you’ve got multiple state variables in use.

So, what happens when we update those variables? The c…


This content originally appeared on DEV Community and was authored by Dan Stanhope

If you've ever built a component in React, chances are you've used state variables. If you've ever build a kinda-complex component in React, chances are you've got multiple state variables in use.

So, what happens when we update those variables? The component re-renders, right? Make changes to a bunch of state variables and a bunch of re-rendering happens. All this rendering could have performance implications for your app.

Introducing automatic batching. Now, batching has been around for a bit in React. But, React only automatically batched state changes for you if they were called inside a hook or browser event. Like say, a click event:

Image description

Console Output:

Image description

This is automatic batching. React takes multiple state changes and groups them together so they don't happen independently -- fantastic stuff.

So where's the improvement?

There are other places you may want to change state in your component(promises, timeouts). Let's say we have a component that fetches some data after a button click. We have two state variables, an array of users and a page counter. We want to update these inside a promise once the data is returned. In React 17, this will cause the component to re-render twice.

Image description

Console Output React 17:

Image description

Console Output React 18:

Image description

This is great! You can make changes to a couple state variables and React will apply them all at the same time, automatically, for you. Awesome!

If you weren't aware of how batching worked in previous versions of React, hopefully you know the limitations now. And if you've got some components out there changing state variables inside promises, might be time to upgrade :)

Thanks!


This content originally appeared on DEV Community and was authored by Dan Stanhope


Print Share Comment Cite Upload Translate Updates
APA

Dan Stanhope | Sciencx (2022-03-31T14:57:49+00:00) What’s Automatic Batching? React 18 feature explained. Retrieved from https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/

MLA
" » What’s Automatic Batching? React 18 feature explained." Dan Stanhope | Sciencx - Thursday March 31, 2022, https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/
HARVARD
Dan Stanhope | Sciencx Thursday March 31, 2022 » What’s Automatic Batching? React 18 feature explained., viewed ,<https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/>
VANCOUVER
Dan Stanhope | Sciencx - » What’s Automatic Batching? React 18 feature explained. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/
CHICAGO
" » What’s Automatic Batching? React 18 feature explained." Dan Stanhope | Sciencx - Accessed . https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/
IEEE
" » What’s Automatic Batching? React 18 feature explained." Dan Stanhope | Sciencx [Online]. Available: https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/. [Accessed: ]
rf:citation
» What’s Automatic Batching? React 18 feature explained | Dan Stanhope | Sciencx | https://www.scien.cx/2022/03/31/whats-automatic-batching-react-18-feature-explained/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.