Opt-out of Automatic Batching with ReactDOM.flushSync

Michael Chan
InstructorMichael Chan
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 2 years ago

In React 18, useState update calls are batched for improved performance. A new API — ReactDOM.flushSync — lets us selectively escape batching. This option is useful when reading the DOM after state changes. Use ReactDOM.flushSync to exempt specific state updates from batching.

Instructor: [0:00] ReactDOM 18 provides a way to opt out of automatic batching of state updates. Because of automatic batching, these two state updates always happen in the same rerender. We can split them using ReactDOM flushSync, simply wrap the update in ReactDOM.flushSync.

[0:26] With this, we're separating these updates by forcing this one to flush to the DOM first. Now, every time we click our button, we see two individual updates. We can repeat this process for as many state updates as we like.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today