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

时间:2017-06-23 19:22:33

标签: javascript reactjs 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.

中最新对象中的属性

1 个答案:

答案 0 :(得分:0)

此语句缺少返回,或者在源代码中没有执行任何操作,但返回默认状态

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

这里发生的是你创建了一个新对象,为它分配了状态,然后什么都没有。 switch语句以返回默认状态结束。您在return

之前缺少Object.assign()