I am using an async function with prevState. I am using prevState for a couple of purposes: 1) I am updating a deeply nested object 2) I am using current state to send data to an API. I am using async for the API call.
This is a simplified version of my code:
this.setState(async prevState => {
//code using await to pull data from an Api
return ({
data: data
})}, function(){
//Here I do not see the updated state
console.log(this.state)
}
)
Without using async, it works fine, but then I cannot make use of await. How can I access my updated state from within the callBack function?
Further Details
Ultimately what I am trying to do is have three nested setState functions as follows. When a user page is saved the savePage() function will be triggered. In that function I want to:
- LOCK THE PAGE IN STATE: Add the page_id to a state array of locked pages. This will prevent further changes to the page.
- SEND DATA TO SERVER AND UPDATE STATE ON SUCCESS: In the callback I am then calling the Api to update the page. AND then update the page status as "saved" in the new setState
- UNLOCK THE PAGE: In the callback from the new setState function, I then issue a third setState function to remove the lock from the locked_pages array located in state. This probably can be combined with step 2.