admin管理员组文章数量:1023187
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
1 Answer
Reset to default 7Assuming your state has the following format:
{
open: [
true,
false,
true,
...
]
}
I believe this should work:
const newState = update(this.state , {
open: {
8: {$set: false}
}
})
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
1 Answer
Reset to default 7Assuming your state has the following format:
{
open: [
true,
false,
true,
...
]
}
I believe this should work:
const newState = update(this.state , {
open: {
8: {$set: false}
}
})
本文标签: javascriptReact immutability helperset new value at index in arrayStack Overflow
版权声明:本文标题:javascript - React immutability helper - set new value at index in array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745542883a2155263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论