[]previous and next page

batch and flushSync

batch(fn) runs fn and defers effects until the outermost batch ends, then flushes synchronously.

For agentsUpdated

batch(fn) runs fn and defers effects until the outermost batch ends, then flushes synchronously. It returns what fn returns. Reads inside the batch see the new values at once. Nested batches count as one.

flushSync(fn?) runs fn (if given) in a batch, then flushes every pending effect before returning, and returns what fn returned. Calling it while a flush is already running (inside an effect) does nothing and warns FLUSH_IN_EFFECT.

You need neither inside JSX event handlers, which already run in a batch. You need flushSync when you want effects and DOM updates to have happened before the next line of non-event code, which in practice means tests and code that measures the DOM after a write.