So I have an POST API
that returns the object that was just created and I would like to get the objects information in reactjs
.
createItem(item){
let temp;
// POST to DB
fetch(url(this.props.api), {
method:"POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: item,
isCompleted:false
})
}).then(function (response) {
return response.json()
}).then(function (body) {
temp = body; // got the objects information
console.log(temp);
});
console.log(temp);
this.state.bucket_list.push({
name: item,
isCompleted: false
});
this.setState({bucket_list: this.state.bucket_list});
}
This is what I have but the I can extract the data outside the then
function. After I get the information I would like to setState
and append the newly created object to my state: bucketlist
. But I think due to Javascript's asynchronous issues, I am unable to do this in the correct order. Any ideas?