Ngrx : Cannot assign to read only property 'Property' of object '[Object]'

后端 未结 1 1701
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 03:09

I\'m using ngrx store.

In my state I have to items

export interface ISchedulesState {
  schedulings: ISchedules;
  actualTrips: ISchedule[];
}
         


        
1条回答
  •  鱼传尺愫
    2021-01-05 03:22

    The basic principle of Redux pattern is immutability of state and its parts, because it let's us to detect changes just by object reference instead of comparing whole objects.

    In your reducer, you cannot directly assign a property of state (state.actualTrips =), because change detector (and selectors) would not detect it as changed.

    To modify state, you return a copy of the state with new modifications.

      const time = action.payload;
      return {
          ...state,
          actualTrips: [...(state.schedulings[time] || [])]
      }
    

    0 讨论(0)
提交回复
热议问题