Angular 10/ Ngrx:作为存储的数组 属性 在之前的状态下始终为空

Angular 10/ Ngrx: Array as store property is always emtpy in previous state

我的商店目前出现了一种奇怪的行为。我有一个数组作为我商店的 属性,我想在分派操作时添加一个项目。该项目已正确添加,但不是作为数组中的新项目,而是数组中的唯一项目,因为状态中的数组始终为空。

例如 初始状态:

stopps: []

添加 ID 为 1 的第一站:

stopps: [{ id: 1 }]

添加第二个 id:2:

stopps: [{ id: 2 }]

我的减速器看起来像这样:

const umdispoReducer = createReducer(
  initialUmdispoState,
  on(UmdispoActions.addStopp, (state, {stopp}) => ({...state, stopps: [...state.stopps, stopp]}))
);

我已经 console.logged 状态并且可以看到在添加新的 stopp 时 stopps 数组总是空的。有人知道这是为什么吗?

我的状态是:

export interface State {
  loading: boolean;
  stopps: DispoStopp[];
  sourceTour: Tour;
}

初始状态:

export const initialUmdispoState: State = {
  loading: false,
  stopps: [],
  sourceTour: null
};

减速器:

export function reducer(state: State | undefined, action: Action): State {
  return umdispoReducer(initialUmdispoState, action);
}

看来我的商店总是有初始状态。状态本身是功能模块的状态

// 编辑: 发现了我的错误。解决方案见我的回答

好吧我每次都把初始状态传给reducer

错误:

export function reducer(state: State | undefined, action: Action): State {
  return umdispoReducer(initialUmdispoState, action);
}

正确:

export function reducer(state: State | undefined, action: Action): State {
  return umdispoReducer(state, action);
}