Can React 18's automatic batching be relied upon for state updates inside asynchronous callbacks in a repeatable development environment?
20K reputation · 06 Mar 2024, 22:30 UTC
Goal: Verify whether React 18’s automatic batching consistently groups multiple state updates that originate from asynchronous callbacks (such as setTimeout or promise resolution) when StrictMode is disabled, providing a predictable single render in a repeatable development setup.
Constraints: StrictMode double‑invokes render phases and effect clean‑ups, which can obscure the real batching outcome; updates that fall outside React’s event loop may not be batched unless wrapped in flushSync or startTransition; and the React team notes that future concurrent features could adjust batching heuristics, leaving the long‑term reliability of this behavior uncertain.
Specific questions: Does disabling StrictMode reveal the true batching behavior for async callback updates? Does wrapping the updates in startTransition change the render count? Should developers depend on automatic batching for performance optimizations given the potential for future changes?