Does anybody know if in React it's possible to use an async updater parameter in setState(updater)
? I have the following code which is not working (f
is called but the UI is not updated):
this.setState( async (prevState) => ({
foo: await f(prevState.someData)
}))
Apparently the async
parameter is a problem. I had to use this ugly alternate version:
this.setState( async (prevState) => {
this.setState({
foo: await f(prevState.someData)
})
})
Is there a better way to write the above code?
Then you can setState
twice.
this.setState((prevState) => {
f(prevState.someData);
// Don't change state now.
return {};
})
async f(someData) {
this.setState(prevState) {
// Do something
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With