如何更新 redux 状态下的某个嵌套属性?
假设我只想更新下面对象中的“value”属性。我知道你不应该深度复制以前的状态,但是我如何只更改数组对象中数组中对象的属性?
提前致谢!
market {
shops: [
{
name: 'abc',
items: [
{
name: 'item1',
value: 40,
id: '234rfds32'
},
{}
]
},
{},
{}
]
}
类似于以下内容:
state = {
...state,
shops: [
...state.shops,
shops[index].items = [
...shops[index].items,
]
]
};
最佳答案
像这样的东西会起作用。 (代码看起来很难看,但没有测试)
var shop = state.shops[index];
var items = [...shop.items];
items[<index>].value = 'your value';
shop.items = items;
var shops = [...state.shops];
shops[index] = shop;
state = {
...state,
shops
};
关于javascript - 更新嵌套数组中的属性值(redux 状态),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51963405/