繁体   English   中英

在两个具有相同键但数组作为属性的对象上分配Object.assign(redux)

[英]Object.assign on two objects with same key but array as a property (redux)

减速器:

export default function filterReducer(state=InitialState.filters, action){
    switch(action.type){
        case 'removeFilter':
                switch(action.filter){
                    case 'sources':
                        Object.assign({}, 
                        state, 
                        {
                         sources=state.sources.filter( 
                         source => source==action.filter.sources?false:true
                             )
                          }
                       )
                    default:return state
                }

InitialState.filters=
    filters: {
        sources: ['dasda', 'asd','asdk'],
        years: {from:null, to:null},
        price: {from:null, to:null},
        mileage:{from:null, to:null},
        colors:['white', 'black', 'red', 'grey', 'blue', 'silver']
    }

状态没有更新,我确定这与数组作为属性并将其合并有关。 Object.assign文档中,具有相同属性的对象键将替换为parameters/arguments.最新对象中的属性parameters/arguments.

该语句缺少返回值,或在源代码中不执行任何操作,而是返回默认状态

switch(action.filter){
    case 'sources':
        Object.assign({}, 
            state, 
            {
            sources=state.sources.filter( 
                source => source==action.filter.sources?false:true
            )
            }
        )
    default:return state
}

这里发生的是您创建了一个新对象,为其分配了状态,然后什么也没有。 switch语句以返回默认状态结束。 您缺少Object.assign()之前的return

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM