Does anyone know if React can use the async updater parameter in setState(updater)? I have the following code that does not work (called f, but the user interface is not updated):
this.setState( async (prevState) => ({
foo: await f(prevState.someData)
}))
Apparently, the parameter asyncis 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?
source
share