Leonardo Alves
Leonardo Alves

Reputation: 63

dispatch any action to redux it clears other part of state

When I try to call any other action in redux it is setting one part of the state to it's initialState.

My root reducer looks like this

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

the part of the state that is being cleared is theme.

action dispatched

state diff

Why this actions that have anything to do with theme reducer can change its state.

Theme reducer

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;
  }
}

reducer of dispatched action

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

Upvotes: 0

Views: 189

Answers (1)

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;
  }
}

return state instead of initial state

Upvotes: 2

Related Questions