简体   繁体   English

Redux reducer 被我的 React 组件中的 setState 或 .map() 覆盖

[英]Redux reducer is getting over written by setState or .map() in my React component

When I console.log the this.state.orginalSeries , this.props.demandGraphSeriesDataReducer and newSeries I get all of the same mutated data back.当我 console.log this.state.orginalSeriesthis.props.demandGraphSeriesDataReducernewSeries我得到所有相同的变异数据。

I tried using .map() and .slice() to not mutate the original reducer data but some how it still is getting mutated.我尝试使用 .map() 和 .slice() 来不改变原始的减速器数据,但它仍然是如何变异的。 I can also see using Redux Devtools it is mutating the state of the reducer as well.我还可以看到使用 Redux Devtools 它也在改变减速器的状态。

class DemandSubDemand extends Component {
  constructor(props) {
    super(props);
    this.state = {
      orginalSeries: null
    };
  }
  componentDidMount(){
    const copy = this.props.demandGraphSeriesDataReducer.slice()
    this.setState({
      orginalSeries: copy
    })
  }

  componentDidUpdate(prevProps, prevState) {
    if (this.props.demandGraphSeriesDataReducer!== prevProps.demandGraphSeriesDataReducer) {
      const copy = this.props.demandGraphSeriesDataReducer.slice()
      this.setState({
        orginalSeries: copy
      })
    }
  }

  onValueUpdate(i, value) {
    const newSeries = this.state.orginalSeries.map((line, j) => {
      if(i === j) line.data[i] = line.data[i] + value;
      return line;
    });
  }

render(){
  return <button onClick={()=>this.onValueUpdate(0,100)}> here</button>
}

I think the mutation may be here:认为突变可能在这里:

  onValueUpdate(i, value) {
    const newSeries = this.state.orginalSeries.map((line, j) => {
      if(i === j) line.data[i] = line.data[i] + value;
      return line;
    });
  }

In particular, line.data[i] = is going to mutate the existing line object in the original array.特别是, line.data[i] =将改变原始数组中现有的line对象。 You would need to make copies of every level of nesting in the data for it to not mutate.您需要复制数据中每个嵌套级别的副本,以免发生变异。

I'd strongly encourage you to use the configureStore() function from Redux Starter Kit , which adds a mutation checker by default.我强烈建议您使用Redux Starter Kit 中configureStore()函数,它默认添加了一个变异检查器。 Also, consider using Immer for simpler immutable updates.此外,考虑使用Immer进行更简单的不可变更新。 Redux Starter Kit's createReducer() and createSlice() functions use Immer internally by default. Redux Starter Kit 的createReducer()createSlice()函数默认在内部使用 Immer。

使用JSON.parse( JSON.stringify( this.props.demandGraphSeriesDataReducer ) )代替slice按照 @icepickle 的建议工作

暂无
暂无

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

相关问题 React 组件没有得到由 reducer 设置的 redux 的初始 state - React component not getting redux's initial state set by reducer 在React Redux Reducer中更新数组中的对象获取map()不是函数 - Updating an object within array in react redux reducer getting map() is not a function React + Redux:Reducer不会重新加载组件 - React + Redux : reducer won't reload component React Redux Reducer从状态获取数据? - React redux reducer getting data from state? React Redux,我的组件未连接到Redux Store。 我在另一个组件中测试了动作和减速器,它是否对其他组件有效? - React Redux, my component is not connected to Redux Store. I tested the action and reducer in another component, it does work for other components? React / Redux-为什么我的动作/减速器不起作用? - React/Redux - Why is my action/reducer not working? 尽管我改变了 state,但 Redux 减速器不会强制我的 React 组件重新渲染 - Redux reducer does not force a rerender on my React Component although I mutated the state React Redux -&gt;如何映射我的状态? - React Redux ->How can I map over my state? 为什么我在 React Redux 减速器中收到关于合成事件的警告? - Why am I getting a warning about synthetic events in my React Redux reducer? 用钩子反应功能组件 - 从用减速器编写的 function 获得响应 - React functional components with hooks - getting response from function written in reducer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM