如何在没有变异的情况下更新状态数组中的现有值?

问题描述 投票:0回答:1

我有以下函数来获取currentStatus和id并根据上面的参数更改一个值。我有以下状态。

this.state.column : [
  [
    arr1 : { id: 1, currentStatus: ''to-do'}, 
           { id: 4, currentStatus: ''completed'},
  ],
  [
    arr2 : { id: 10, currentStatus: ''in-progress'}, 
           { id: 14, currentStatus: ''completed'},
  ],
]

我将新的currentStatus和特定的id作为参数提供给以下函数,并且需要用新的currentStatus替换现有的currentStatus。 id可以找到特定的对象(整个数组是唯一的)。我正在尝试以下代码,最后得到错误Cannot read property 'nn' of undefined

import update from 'immutability-helper';

getCurrentStatus = (currentStatus, id) => {
    Object.keys(this.state.columns).forEach(ee => {
      if (this.state.columns[ee].find(x => x.id === id)){
        var nn = this.state.columns[ee].findIndex(x => x.id === id)
        this.setState({
          columns : update(this.state.columns, {ee:{nn:{$set : currentStatus}}})
        }, () =>
          console.log(this.state.columns)
        )
      }
    })
  }
reactjs
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.