How to handle the default case when using 'createReducer' to create a reducer and 'on' to handle actions?

18 views Asked by At

This is how we created reducers before the createReducer helper method was introduced:

export function reducer(state: AppState, action: Action) {
 switch (action.type) {
   case "[Category List] Add Category":
         return { ...state, categories: [...state.categories, action.payload] };
   default:
     return baseReducer;
 }
}

The default case will handle the rest of actions that are common to all the modules.

export function baseReducer(state, action){
 //... COMMON CASES
}

With the helper the above can be written as:

export const reducer = createReducer(
  initialState,
  on(addCategoryRequest, (state, { payload }) => {
     return { 
      ...state, 
      categories: payload
   };
   //WHAT ABOUT THE 'default case'
 }
}

How do I handle default types?

1

There are 1 answers

0
timdeschryver On

The createReducer doesn't support default cases. One option is to create a metareducer that wraps the createReducer.