javascript - React immutability helper - set new value at index in array - Stack Overflow

Is there any way I can set value of array in specific index with React imutability helper ? For example

Is there any way I can set value of array in specific index with React imutability helper ?

For example something like this:

   const newState = update(this.state , {
        open[8]:{$set: false}
    })
    this.setState(newState);

or do I have to deep clone that array using JSON and then set new value at specific index and then use react imutability helper to update state?

Is there any way I can set value of array in specific index with React imutability helper ?

For example something like this:

   const newState = update(this.state , {
        open[8]:{$set: false}
    })
    this.setState(newState);

or do I have to deep clone that array using JSON and then set new value at specific index and then use react imutability helper to update state?

Share Improve this question asked Feb 23, 2017 at 16:48 ĽubomírĽubomír 1,2112 gold badges13 silver badges22 bronze badges 1
  • You could use { open: state.open.filter((value, index) => index != 8) } – Aaron Beall Commented Feb 23, 2017 at 20:37
Add a ment  | 

1 Answer 1

Reset to default 7

Assuming your state has the following format:

{
  open: [
    true,
    false,
    true,
    ...
  ]
}

I believe this should work:

const newState = update(this.state , {
  open: {
    8: {$set: false}
  }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745542886a4632208.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信