javascript - How to access value based on index from array defined in state ReactJS -


i working on reactjs.my state looks this

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}) } 

now want access values index tried this:

<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> 

i may have searched wrong in google. not getting proper soln

make click handler this

clickhandler(e, index) {    let open = [...this.state.open];    open[index] = !open[index];    this.setstate({ open }); } 

and call click handler so...

onclick={e => this.clickhandler(e, index)}     

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -