tezvyn:

How does React batch multiple setState calls in one event handler?

Source: react.devintermediate

Tests your grasp of React batching and stale closures. React queues updates until the handler exits, so multiple literal setState calls with the same stale value update once, while updater functions queue sequentially.

Tests your understanding of React's automatic batching and state snapshots in event handlers. A strong answer covers four things in order: first, React waits until the handler exits before processing updates to avoid partial renders; second, because state is fixed per render, multiple literal setState calls using the same variable only update once; third, updater functions queue sequential changes by receiving the previous queued value; fourth, React does not batch across separate intentional events like distinct clicks.

Read the original → react.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How does React batch multiple setState calls in one event handler? · Tezvyn