ReactJS – use asynchronous update parameters React, setState?

Does anyone know whether the async updater parameter can be used in setState(updater) in React? I have the following code invalid (f is called, but the UI is not updated):

this.setState( async (prevState) => ({
foo : await f(prevState.someData)
}))

Obviously the async parameter is the problem. I had to use this ugly alternative version:

< pre>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
}
}< /pre>

Does anyone know whether it is possible to use the async updater parameter in setState(updater) in React? I have the following code invalid (f is called, but the UI is not updated):

this.setState( async (prevState) => ({
foo : await f(prevState.someData)
}))

Obviously the async parameter is the problem. I had to use this ugly alternative version:

< pre>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
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5949 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.