[英]How to access value based on index from array defined in state ReactJS
我正在研究 reactjs.my state 看起来像这样
constructor(props) {
super(props);
this.state = {
open: []
};
}
componentWillReceiveProps(nextProps) {
let {open} = this.state;
for(var i=0;i<nextProps.groupB.length;i++){
open[i]=false;
}
this.setState({open:open})
}
现在我想通过索引访问这些值,所以我尝试这样:
<div className="panel-body">
{this.props.groupB.map((data,index) =>
<div className="panel panel-default">
<Button
onClick={() =>
this.setState({
open: !this.state.open[index]
})}
>
<Glyphicon
glyph={
this.state.open[index]
? 'glyphicon glyphicon-minus'
: 'glyphicon glyphicon-plus'
}
/>
</Button>
</div>
</div>
<Collapse in={this.state.open}>
<Well>//...some code
</Well>
</Collapse>
</div>
我可能在谷歌搜索错了。 但没有得到适当的解决方案
制作这样的点击处理程序
clickHandler(e, index) {
let open = [...this.state.open];
open[index] = !open[index];
this.setState({ open });
}
然后像这样调用那个点击处理程序......
onClick={e => this.clickHandler(e, index)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.