Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nested Redux更新嵌套状态数组_Nested_Redux_React Redux - Fatal编程技术网

Nested Redux更新嵌套状态数组

Nested Redux更新嵌套状态数组,nested,redux,react-redux,Nested,Redux,React Redux,嗨,我想问两个关于redux的问题 如何更新嵌套在对象内部数组中的对象,示例如下 newInStockList:{ newEntry:[ 0:{ id:"584f9b926f69ee93d4eb320d", name:"apple", amount:"10" }, 1:{ id:"584f9b926f69ee93d4eb31e5",

嗨,我想问两个关于redux的问题

  • 如何更新嵌套在对象内部数组中的对象,示例如下

    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;
            }
        }