Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setState being called synchronously #14422

Closed
jasimon opened this issue Dec 11, 2018 · 4 comments
Closed

setState being called synchronously #14422

jasimon opened this issue Dec 11, 2018 · 4 comments

Comments

@jasimon
Copy link

jasimon commented Dec 11, 2018

Do you want to request a feature or report a bug?
bug

What is the current behavior?
setState is merging state synchronously when called from asynchronous code (i.e. a .then on an ajax request)

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
https://jsfiddle.net/Luktwrdm/1456/

What is the expected behavior?
setState should be merging state asynchronously I think?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
react 16.4.1, unsure if it was in previous versions (the code affected is new)

@TrySound
Copy link
Contributor

TrySound commented Dec 11, 2018

@jasimon Currently setState calls are batched only in react event handlers. In other cases it's called synchronously. This is expected behaviour. But do not rely on it please. React v17 will enable concurrent mode by default so all setState calls will be asynchronous and code written that way will be broken.

@jasimon
Copy link
Author

jasimon commented Dec 11, 2018

Got it, thanks for your quick response. Is there any way to force batched updates? When I get an HTTP response I have a few distinct calls to setState (some happen conditionally based on the type of the response). I'm relying on batching to have those changes all happen atomically to the component. (This issue is actually presenting itself through redux dispatching multiple actions in a then callback so it's not as simple as building a payload in the callback and passing that at once to setState).

@markerikson
Copy link
Contributor

markerikson commented Dec 12, 2018

@jasimon : try wrapping that chunk of code in ReactDOM.unstable_batchedUpdates().

You may also want to look at the Redux FAQ entry on reducing UI update notifications.

@jasimon
Copy link
Author

jasimon commented Dec 12, 2018

Ah, thanks! Will close, seems to be working as intended

@jasimon jasimon closed this as completed Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants