添加属性reducer redux

时间:2016-03-09 10:16:39

标签: redux reducers

我不想在reducer中向我的对象sections: []添加属性formOpen,我从我的服务器接收具有其他属性的对象formOpen,我想添加这一个,我怎么能在这里做到这一点? 感谢

import { combineReducers } from 'redux'
import * as types from '../constants/ActionTypes'

const initialState = {
    isFetching: false,
    formOpen: {

    }
};

export function formEditor (state = initialState, action) {
    switch (action.type) {
        case  types.RECEIVE_OPEN_FORM:
            return {
                ...state,
                isFetching: false,
                formOpen: action.formOpen
            };        

        default:
            return state;
    }
}

export default combineReducers({
    formEditor
})

1 个答案:

答案 0 :(得分:5)

这应该可以解决问题:

export function formEditor (state = initialState, action) {
    switch (action.type) {
        case  types.RECEIVE_OPEN_FORM:
            return {
                ...state,
                isFetching: false,
                formOpen: action.formOpen
            };        

        case types.SET_FORM_OPEN_SECTIONS:
            return {
                ...state,
                isFetching: false,
                formOpen: {
                  ...state.formOpen,
                  sections: action.formOpenSections
                }
            };

        default:
            return state;
    }
}