Redux嵌套状态

时间:2017-04-10 15:58:46

标签: reactjs redux react-redux

我已阅读了很多文章和示例,但我找不到简化嵌套状态的方法。是否可以通过属性(如下面的部分代码)来使用静态运算符来更新状态?

const initialState = {
  currentTag: 'fr-FR',
  locales: {
    components: [],

  },
};

const setComponentsLocales = (state, payload) => ({
  ...state,
  [payload.oid]: payload.locales,
});

const localesReducer = (state, action) => {
  switch (action.type) {
    case types.SET_COMPONENT_LOCALES:
      return {
        ...state,
        components: setComponentsLocales(state.components, action.payload),
      };
    default:
      return state;
  }
};

export default (state = initialState, action) => {
  switch (action.type) {
    case types.SET_LANGUAGE:
      return {
         ...state,
         currentTag: action.payload.tag,
      };
    default:
      return {
        ...state,
        locales: localesReducer(state.locales, action),
      };
   }
};

0 个答案:

没有答案