An app that checks how batching works in React 18.2. The app Uses two states variables: a counter that is incremented, and a flag that is toggled between 0
and 1
. A button when it is clicked, the states are updated, and the GIF
below shows how many times a re-render takes place.
Then, the states are updated inside a timeout, and the GIF
below shows how many re-renders take place.
We notice that in the very first state updates, the re-render takes place twice, this is due to the fact that we are using Strict Mode
, and in the next state updates, it will be batched and takes place only once.