Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据状态JS中定义的数组中的索引访问值_Javascript_Reactjs_Ecmascript 6 - Fatal编程技术网

Javascript 如何根据状态JS中定义的数组中的索引访问值

Javascript 如何根据状态JS中定义的数组中的索引访问值,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,我正在做reactjs。我的状态是这样的 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;

我正在做reactjs。我的状态是这样的

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})
}
clickHandler(e, index) {
   let open = [...this.state.open];
   open[index] = !open[index];
   this.setState({ open });
}
构造函数(道具){
超级(道具);
此.state={
打开:[]
};
}
组件将接收道具(下一步){
设{open}=this.state;
对于(var i=0;i
这是我的国家({
打开:!this.state.open[索引]
})}
>


//…一些代码

我可能在谷歌上搜索错了。但是没有找到合适的解决方案。做一个这样的点击处理程序

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})
}
clickHandler(e, index) {
   let open = [...this.state.open];
   open[index] = !open[index];
   this.setState({ open });
}
然后像这样调用单击处理程序

onClick={e => this.clickHandler(e, index)}    

制作一个如下的点击处理程序

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})
}
clickHandler(e, index) {
   let open = [...this.state.open];
   open[index] = !open[index];
   this.setState({ open });
}
然后像这样调用单击处理程序

onClick={e => this.clickHandler(e, index)}    

只是一个注释,如果可能的话,你应该避免变异状态,因此你应该使用
let open=[…this.state.open];
而不是
let{open}=this.state;
只是一个注释,如果可能的话,你应该避免变异状态,因此你应该使用
let open=[…this.state.open];
而不是
let{open}=this.state;
我应该在Glyphicon类中做什么修改?我认为这是可以的,一件事,为什么不映射通过
this.state.open
?我的整个面板变得不可见:(…wait将添加更多代码,以便对您有意义..updated,因为上面的映射提供了值,您正在通过调用
this.clickHandler(…)访问您的单击处理程序
在你的匿名函数中;你不需要将它绑定到
这个
。我应该在Glyphicon类中做什么修改?我认为这是可以的,一件事,为什么不映射到
这个.state.open
?我的整个面板变得不可见:(…wait将添加更多代码,以便对您有意义..更新,因为上面的映射提供了值,您正在通过调用
this来访问单击处理程序。在匿名函数中单击处理程序(…)
;您不需要将其绑定到
this