最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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}
  }
})
发布评论

评论列表(0)

  1. 暂无评论