question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Out-of-order updates with React Concurrent Mode

See original GitHub issue

Bug

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.

Kapture 2019-12-17 at 11 39 22

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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
gaearoncommented, Apr 7, 2021

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.

2reactions
kyarikcommented, Apr 6, 2020

@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 a UserBlockingPriority, which is a high priority, but not the highest.

On the other hand, ReactDOM.flushSync and ReactDOM.flushControlled run the function you provide with ImmediatePriority, which is the highest priority. ReactDOM.flushControlled is still unstable, so it’s available as unstable_flushControlled.

Therefore, a possible fix to the issue is to wrap setValue(val) in either of those two functions. So,

const handleChange = useCallback(val => {
  ReactDOM.flushSync(() => {
    setValue(val);
  });
}, []);

or

const handleChange = useCallback(val => {
  ReactDOM.unstable_flushControlled(() => {
    setValue(val);
  });
}, []);

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found