【发布时间】:2018-09-28 07:46:47
【问题描述】:
我已将数组的状态设置为 [] 空,在 componentDidMount 中我正在获取 API 响应。现在我想用新的 json 响应数组设置空数组的状态我该怎么做?
constructor(props)
{
super(props)
this.state = {
categoryList : []
}
}
componentDidMount()
{
axios.get('http:xxxxxxxxxxxxxxxxxxxxxxx')
.then(res =>
{
this.setState = {
categoryList : res.data.body.category_list(json path)
}
})
.catch(function(error) {
console.log('Error on Authentication' + error);
})
}
【问题讨论】:
-
以上代码有什么问题。
-
我想将 categoryList 的状态设置为 Api Json 响应.. 只想用 json 响应更新空数组..
-
setState是一个方法,需要用object/updater函数调用,去掉
=,像这样:this.setState({ categoryList : res.data.body.category_list(json path) }) -
哎哟...对不起,我没有正确检查代码...谢谢你
标签: reactjs