I am running a function that has an API call that brings back a response that I use to set the state and then triggers another function which uses the state that has just been updated. the problem the second function is running before response comes back and updated the state
I have tried .then I thought that should work
import React from "react";
import API from "../../utils/API"
class Content extends React.Component {
state={
postID:"",
statusPost:""
}
submitPost = () => {
API.savePost({
content: this.state.statusPost,
post_by: this.props.userInfo.firstname + this.props.userInfo.lastname
})
.then(console.log(this.submitPost))
.then(res => {
this.setState({ postID:res.data._id })
console.log(this.state)
})
.then(this.addPostID())
.catch(err => console.log(err));
}
addPostID = () => {
API.postID({
_id: this.props.userInfo.user_ID,
post:this.state.postID
})
.then(res => console.log(res))
.catch(err => console.log(err));
}
}