javascript - 即使指定了索引,对象中的所有数组值也会在 this.setState 中更新
问题描述
我有一个动态选项表。选择表单类型时,它将基于数组索引更新特定的数组类型。例如:this.state.menuInput = [{"title" : "array 1" , "type" : ""} , {"title" : "array 2" , "type" : ""}]
。当我更改数组 2 值的表单类型时,类型应该更新为所选的任何内容。
它应该是这种格式[{"title" : "array 1" , "type" : ""} , {"title" : "array 2" , "type" : "radio_button"}]
。目前,当我改变状态时,两个数组中的“类型”都在更新。
像这样: [{"title" : "array 1" , "type" : "radio_button"} , {"title" : "array 2" , "type" : "radio_button"}]
。即使我专门将数组索引硬编码在setstate
.
我怎样才能根据索引改变数组值?
menuTypeOption(index, menuType){
this.setState(prevState => {
const newMenu = [...prevState.menuInput];
newMenu[index]['title'] = menuType;
return {menuInput: newMenu};
} , function(){
console.log(this.state.menuInput);
})
}
解决方案
在这一行const newMenu = [...prevState.menuInput];
中,您不会制作数组的深层副本,而只会制作指向相同对象的新数组。
所以newMenu[index]['title'] = menuType;
改变原始对象。
您可以创建具有更改属性的新对象,而不是变异:
newMenu[index] = {...newMenu[index], title: menuType};
如果原始数组中已经有两个指向同一个对象的指针,那么这个解决方案也可以工作,因为它会创建新对象,并且不会改变共享对象。
可以这样证明:
const obj = {foo: 'bar'};
let arr = [obj, obj]; // same object used twice
// now if we do `obj.foo = 'new value';`, it'll change both values in array
arr = [...arr];
arr[1] = {...arr[1], foo: 'new value'};
// now we have two different objects in arr: one points to old, and one points to new copy with changed values
推荐阅读
- node.js - 如何在 mongodb 中查询 3 级嵌套对象
- android - Android Studio 线性专栏
- android - Android 不良做法:缺少 Google Play 服务更新的安全提供程序修复工作(小时):0.1
- php - Laravel:SQLSTATE [HY000]:一般错误:1005 无法创建表
- jolt - JOLT 嵌套 if 条件
- javascript - 重定向到下一页后如何在控制台中自动运行javascript
- c# - 选择 EF 中枚举的数值
- google-slides-api - 是否可以将形状从幻灯片移动到另一个?
- reactjs - Azure App Service 在部署 REACT JS 应用程序时出现错误
- reactjs - 获取 webTorrent 以播放存储在 AWS 私有 s3 存储桶中的 .mp4 文件