Skip to content

Nested asynchronous setState and setState inside Promises #3734

Discussion options

You must be logged in to vote

I have a component acting as a button. The $Love code makes an HTTP fetch and returns a promise. The Preact 8 code looked something like this: (...)

To be honest the code looks perfectly fine and I'd keep the same for Preact 10. The text in the upgrade guide refers to the fact that you cannot immediately read the new values from this.state. In both Preact 8.x and 10.x the update is applied asynchronously to the DOM.

state = { foo: false }

// Preact 8.x
this.setState({ foo: true });
console.log(this.state.foo) // Logs: true

// Preact 10.x
this.setState({ foo: true });
console.log(this.state.foo) // Still logs previous value: false

That's what that section is referring to.

The setState

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mikekasprzak
Comment options

@marvinhagemeister
Comment options

Answer selected by mikekasprzak
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants