Out-of-order updates with React Concurrent Mode
See original GitHub issueBug
What’s the current behavior?
When using Slate with Concurrent React (via ReactDOM.createRoot) on the experimental release channel), updates (to the node list?) can occur out of order resulting in text being appended after the cursor.
https://codesandbox.io/s/slate-050-basic-x-concurrent-mode-bug-qnxqu
Slate: 0.55.3
React: Experimental, 16.8.6 with unstable_createRoot
Browser: Chrome
OS: Mac
What’s the expected behavior?
Updates stay in order no matter how fast the user types.
Suggested Solutions
It seems like this could have to do with either batchedUpdates or scheduler, but I’m not exactly sure. I don’t know enough about the internals of Slate to give more detail, but worth getting ahead of this IMHO.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:15 (8 by maintainers)
Top Results From Across the Web
Introducing Concurrent Mode (Experimental) - React
This page provides a theoretical overview of Concurrent Mode. ... Once they start rendering an update, including creating new DOM nodes and running...
Read more >React v18.0 – React Blog
In a concurrent render, this is not always the case. React may start rendering an update, pause in the middle, then continue later....
Read more >Adopting Concurrent Mode (Experimental) - React
Concurrent Mode is only available in the experimental builds of React. ... They let everyone decide when a migration is worth it, and...
Read more >Concurrent UI Patterns (Experimental) - React
Usually, when we update the state, we expect to see changes on the screen immediately. ... Concurrent Mode offers a new set of...
Read more >Understanding React Concurrent Mode: Part 1 - Yash Mahalwal
React re-renders after every state update. An app exists as a hierarchy of components (in the form of a tree). When a component...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Basically, we now run updates synchronously by default, and even if we’re going to relax this in some future majors for some events, we’ll keep that behavior for keyboard, clicks, and so on. And then if you want to opt out,
useTransition
is your entry point to time slicing.@jaredpalmer Thanks for mentioning
ReactDOM.unstable_discreteUpdates
. That led me to do some research.discreteUpdates
definitely helps, but as you pointed out, it’s not perfect, as there are still some cases in which the cursor gets left behind.ReactDOM.unstable_discreteUpdates
runs the function you provide with aUserBlockingPriority
, which is a high priority, but not the highest.On the other hand,
ReactDOM.flushSync
andReactDOM.flushControlled
run the function you provide withImmediatePriority
, which is the highest priority.ReactDOM.flushControlled
is still unstable, so it’s available asunstable_flushControlled
.Therefore, a possible fix to the issue is to wrap
setValue(val)
in either of those two functions. So,or
With this change, the original issue seems no longer reproducible.
BTW, you misspelled @gaearon, so I don’t think he got notified about this thread.