State batching là React gộp nhiều setState calls lại và chỉ re-render một lần.
- Trong React 17, chỉ batch trong event handlers.
- React 18 giới thiệu Automatic Batching, batch cả trong setTimeout, Promise, và native event handlers.
- Dùng
flushSync()để opt-out batching khi cần update ngay lập tức.
State batching means React groups multiple setState calls together and triggers only one re-render.
- In React 17, batching only happened inside event handlers.
- React 18 introduced Automatic Batching, which extends batching to setTimeout, Promises, and native event handlers as well.
- Use
flushSync()to opt out of batching when you need an immediate update.