Remember the earlier versions of React that used to batch multiple state updates inside event handlers such as click or change to avoid multiple re-re

Automatic batching in React 18 helps avoid re-rendering

submited by
Style Pass
2022-01-13 15:30:04

Remember the earlier versions of React that used to batch multiple state updates inside event handlers such as click or change to avoid multiple re-renders? React 18 has added automatic batching for all use cases to improve that performance optimization even further.

For people who do not know about batching, let us discuss that first. If a function is applying multiple updates to the state of a component, react batches or combines them into one state update and a single render on the browser.

From React 18, there is a new root API, called createRoot. This allows us to use the concurrent features that were introduced in React 18.

In all the following examples, the multiple calls to set state would be batched at once. This avoids re-rendering with partial states after a render.

There are going to be cases where we do not want React to batch update the state. We can make use of the flushSync API from react-dom.

Leave a Comment