这就是我们在引入
createReducer
辅助方法之前创建减速器的方式:
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;
}
}
默认情况将处理所有模块通用的其余操作。
export function baseReducer(state, action){
//... COMMON CASES
}
使用helper,上面可以写成:
export const reducer = createReducer(
initialState,
on(addCategoryRequest, (state, { payload }) => {
return {
...state,
categories: payload
};
//WHAT ABOUT THE 'default case'
}
}
如何处理默认类型?
createReducer
不支持默认情况。
一种选择是创建一个包装 createReducer
的元缩减器。