Nested Redux更新嵌套状态数组
嗨,我想问两个关于redux的问题Nested Redux更新嵌套状态数组,nested,redux,react-redux,Nested,Redux,React Redux,嗨,我想问两个关于redux的问题 如何更新嵌套在对象内部数组中的对象,示例如下 newInStockList:{ newEntry:[ 0:{ id:"584f9b926f69ee93d4eb320d", name:"apple", amount:"10" }, 1:{ id:"584f9b926f69ee93d4eb31e5",
newInStockList:{
newEntry:[
0:{
id:"584f9b926f69ee93d4eb320d",
name:"apple",
amount:"10"
},
1:{
id:"584f9b926f69ee93d4eb31e5",
name:"orange",
amount:"20" <-------- target
},
]
}
newInStockList:{
新条目:[
0:{
id:“584f9b926f69ee93d4eb320d”,
名称:“苹果”,
金额:“10”
},
1:{
id:“584f9b926f69ee93d4eb31e5”,
名称:“橙色”,
数量:“20”在做了更多的研究后,我意识到
线程通过使用不变性帮助()遇到了与我类似的问题
代码更新:
import update from 'react-addons-update';
export var newInStockList = (state = {newEntry:[]}, action)=>{
switch (action.type){
case 'INSERT_NEW_ITEM_TO_INSTOCK_LIST':
return {
newEntry:[...state.newEntry, action.item]
};
case 'REMOVE_ITEM_FROM_INSTOCK_LIST':
return {
newEntry:[
...state.newEntry.slice(0, action.targetItemIndex),
...state.newEntry.slice(action.targetItemIndex + 1)
]
}
case 'EDIT_ITEM_FROM_INSTOCK_LIST':
return update(state,{
newEntry:{
[action.targetItemIndex]:{
amount:{$set : action.amount}
}
}
})
default:
return state;
}
}
可能重复的
import update from 'react-addons-update';
export var newInStockList = (state = {newEntry:[]}, action)=>{
switch (action.type){
case 'INSERT_NEW_ITEM_TO_INSTOCK_LIST':
return {
newEntry:[...state.newEntry, action.item]
};
case 'REMOVE_ITEM_FROM_INSTOCK_LIST':
return {
newEntry:[
...state.newEntry.slice(0, action.targetItemIndex),
...state.newEntry.slice(action.targetItemIndex + 1)
]
}
case 'EDIT_ITEM_FROM_INSTOCK_LIST':
return update(state,{
newEntry:{
[action.targetItemIndex]:{
amount:{$set : action.amount}
}
}
})
default:
return state;
}
}