reactjs - SetState 空数组与对象的 Json 响应数组?
问题描述
我已将数组的状态设置为 [] 空,在 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);
})
}
解决方案
尝试这个:
this.setState({
categoryList : res.data.body.category_list(json path)
})
因为setState()
方法接受将与当前状态合并的对象。
推荐阅读
- java - Kafka POM 依赖版本问题
- enterprise-architect - Enterprise Architect Schema Composer 根类基数
- php - Laravel Eloquent concat 和 groupBy() 并返回每个组的一个标题
- python - 如何使用 json.dumps 将带有花括号的 dict 值转储到 JSON?
- java - 下载后如何通过意图打开文件?
- time-complexity - 嵌套循环和 Dijkstra 算法的大 O 表示法
- javascript - 将 URL 保存到 cookie 并适应按钮
- mysql - MySQL 到 HiveQL 转换中的错误,包括 DATE_SUB 和 INTERVAL
- vba - VBA 颜色单元格取决于值
- javascript - 带有选定列脚本的 Google 表格单独的日志页面