如何通过setState更改getInitialState中的对象中的项?
例如:
getInitialState: function() {
return {list: [
{id:'1',
title: 'The Baby Boss',
year: '2017',
quality: 'Blu-ray',
stars: ['Fred Astaire, ', 'Humphrey Bogart, ', 'Marlon Brando, ', 'Richard Burton, ', 'Charlie Chaplin'],
Show: false
},
{
id:'2',
title: 'Pirates of the Caribbean: Dead Men Tell No Tales ',
year: '2016',
quality: 'HDrip',
stars: ['John Depp, ', 'Orlando Bloom, ', 'Javier Bardem, ', 'Kaya Scodelario, ', 'Brenton Thwaites'],
Show: true
}`
Run Code Online (Sandbox Code Playgroud)
更清晰.我已创建getInitialState并render编辑它,并在屏幕上显示列表对象.我有按钮,我想点击它并改变其中一个对象的smth.通过列表中的项目我使用'map'功能.结果,我试图像下面的代码一样做:
this.setState({
film.Show: false
})
Run Code Online (Sandbox Code Playgroud)
但这种方式不起作用.达到我的目标的哪种方式是正确的?感谢您的想法.
您需要找到目标项并更新:
someEventHandler: function(targetId) {
this.setState({
list: this.state.list.map(function(film) {
if (film.id === targetId) {
film.Show = false;
}
return film;
})
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
153 次 |
| 最近记录: |