【发布时间】:2017-10-29 20:40:10
【问题描述】:
所以我有一个POST API,它返回刚刚创建的对象,我想在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});
}
这是我所拥有的,但我可以在 then 函数之外提取数据。获得信息后,我想setState 并将新创建的对象附加到我的state: bucketlist。但我认为由于 Javascript 的异步问题,我无法以正确的顺序执行此操作。有什么想法吗?
【问题讨论】:
标签: javascript reactjs