我喜欢使用 createReducer 创建 reducer 的新方法,但我无法找到任何迁移路径,如何将旧 reducer 迁移到这个新函数。
我想以 createReducer 方式添加新的状态突变,并结合两个 reducer ,然后加类我移动东西。
知道该怎么做吗?
最佳答案
阅读Mixing Action Styles In NgRx State作者:布兰登·罗伯茨。
import { createReducer, on, union } from '@ngrx/store';
import * as LegacyCounterActions from './legacy-counter.actions';
import * as CounterActions from './counter.actions';
export const initialState = 0;
type State = number;
const counterReducer = createReducer(initialState,
on(CounterActions.increment, state => state + 1)
);
const CounterActionsUnion = union({...CounterActions});
type Actions =
| LegacyCounterActions.Union
| typeof CounterActionsUnion;
export function reducer(state: State | undefined, action: Actions) {
switch(action.type) {
case LegacyCounterActions.CounterActionTypes.Increment:
return state + 1;
default:
return counterReducer(state, action);
}
}
关于angular - 如何使用旧的reducer函数添加createReducer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62473411/