简体   繁体   English

避免NgRx Reducer中的状态突变

[英]Avoid State Mutation in NgRx Reducer

I'm new to Redux/NgRx and also fairly new to JavaScript. 我是Redux/NgRx ,也是JavaScript的新手。 Trying to implement a sample state change. 尝试实施样本状态更改。 I understand that when I dispatch an action, in the reducer, I should be creating a new state from previous state and update part of it and return. 我了解到,当我在化简器中调度动作时,应该从以前的状态创建一个新状态,并更新它的一部分并返回。 Thus not to mutate the existing state. 从而不改变现有状态。 However, with my code below, I always see in the dev tools that my state is being mutated instead of new version. 但是,在下面的代码中,我总是在开发工具中看到我的状态正在突变,而不是新版本。 What is incorrect in the reducer? 减速器有什么不正确的地方?

Reducer: 减速器:

export interface State {
  transactions: []
}

export function trasactionReducer (state: State, action: txActions.Actions) {

  switch (action.type) {
    case txActions.LOAD_ACCOUNT_TRANSACTION:
      return {...state};

    case txActions.STORE_ACCOUNT_TRANSACTION:
      return {...state, 
        transactions : action.payload
      };

    default:
      console.log("transactionReducer, defaul action triggerred for action: "+ action.type);
      return state;
  }

}

As already said in the comments, I think you aren't mutating the state directly. 正如评论中已经说过的,我认为您不是直接改变状态。 If you want to be sure, you can use the ngrx-store-freeze package in dev mode, this will throw an error if you do mutate the state. 如果您想确定的话,可以在开发人员模式下使用ngrx-store-freeze包,如果您对状态进行了更改 ,这将引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM