reactjs - 如何更改内部状态的数组
问题描述
我想results
通过推送新数据来更新!!!
data 是状态数组中的一个数组,
this.state = {
comments: {
results: []
}
};
const new = {user : "users"};
var comments= this.state.comments.results[0].data;
var comment= data.push(new);
解决方案
这里的其他答案过于复杂。只需这样做:
this.state = {
comments: {
results: []
}
};
const new = {user : "users"};
var comments = this.state.comments.results[0].data;
this.setState({
comments: {
...this.state.comments,
results: [
...this.state.comments.results,
new
]
}
});
(...
扩展运算符)基本上只获取对象/数组的所有属性/元素并将它们转储到包含的对象/数组中。对于对象,在展开操作之后定义的任何附加属性,如果它们的键相同,则将覆盖由展开添加的属性。
推荐阅读
- elasticsearch - 弹性搜索错误 - 期待双引号开始字段名称
- jquery - JQuery,将url捕获为变量
- symfony - Symfony 实体类中的动态属性
- node.js - Bad response when writing secret using node-vault
- ssl - 尝试将 websocket 连接到安全的客户端端点会引发错误:XNIO000100: 'https' URL scheme selected but no SSL provider given
- vba - 从 VBA 在 IE 中启动 JavaScript 查询
- python-3.x - 课堂上的sklearn方法
- r - 计算元素出现次数和平均值的列
- wcf - .net 核心的 SecurityBindingElement.EnableUnsecuredResponse 等价物?
- powershell - 通过 Powershell 破坏格式字符串进行 SSAS 部署