派遣任何操作到redux,清除状态的其他部分

时间:2018-07-26 12:25:57

标签: reactjs redux

当我尝试在redux中调用其他任何操作时,它会将状态的一部分设置为initialState

我的减根剂看起来像这样

const rootReducer = combineReducers({
    credentials: combineReducers({
    cred,
    user,
    partner,
    merchant,
    bank,
    error,
    auth,
  }),
  preCredentials,
  theme,
});

要清除的状态部分为theme

action dispatched

state diff

为什么与此主题缩减程序有关的操作可以更改其状态。

主题还原器

function theme(state = { ...INITIAL_THEME }, action) {
  switch (action.type) {
    case LOADING_THEME:
      return {
        ...state,
        isLoading: true,
      };
    case SAVE_THEME:
      return {
        ...action.theme,
        error: {
          status: null,
          message: '',
        },
        isLoading: false,
      };
    case CLEAR_THEME:
      return INITIAL_THEME;
    default:
      return INITIAL_THEME;
  }
}

减少派遣行动

function preCredentials(state = { ...INITIAL_STATE }, action) {
  switch (action.type) {
    case SAVE_USERNAME:
      return { ...state,
        user: { ...state.user,
          fullName: action.fullName,
        },
      };
    default:
      return state;
  }
}

1 个答案:

答案 0 :(得分:2)

function theme(state = { ...INITIAL_THEME }, action) {
  switch (action.type) {
    case LOADING_THEME:
      return {
        ...state,
        isLoading: true,
      };
    case SAVE_THEME:
      return {
        ...state,
        ...action.theme,
        error: {
          status: null,
          message: '',
        },
        isLoading: false,
      };
    case CLEAR_THEME:
      return INITIAL_THEME;
    default:
      return state;
  }
}

返回状态而不是初始状态