How to edit an item in a state array?

后端 未结 4 475
逝去的感伤
逝去的感伤 2021-01-31 18:08

So here is my state:

this.state = {
  ids: [\'A\', \'E\', \'C\']
};

How would I go about modifying the state so that \'E\' at index 1 is change

4条回答
  •  心在旅途
    2021-01-31 18:35

    Here is another solution to change a specific index of array in a setState:

    this.setState({
      ...array,
      Object.assign([...array], { [id]: yourNewObjectOrValue })
    })
    

提交回复
热议问题