Bug: setState re-renders the component synchronously when called in a promise #21352
Labels
Resolution: Expected Behavior
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
React version: 16.13.1
Steps To Reproduce
setState
in an asynchronous function afterawait
, or in a.then()
function on a promise.Link to code example: https://codesandbox.io/s/friendly-ganguly-gsl6c?file=/src/App.js
Code example
The current behavior
Calling
setState
will synchronously re-rendener the component - the component function will be called with the new state beforesetState
even returns. The code example prints:The expected behavior
The new state should be queued for an update and then processed afterwards, preferably the same way as if it wasn't in a
.then()
promise function: all the state changes get evaluated, and then the component is re-rendered only once. The code example should print:The text was updated successfully, but these errors were encountered: