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?
The
createReducerdoesn't support default cases. One option is to create a metareducer that wraps thecreateReducer.