Skip to content

Instantly share code, notes, and snippets.

@ovpv
Created March 23, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ovpv/440293d7a04b241e9f4c256e07daf01c to your computer and use it in GitHub Desktop.
Save ovpv/440293d7a04b241e9f4c256e07daf01c to your computer and use it in GitHub Desktop.
updateTabs method
class Accordion extends React.Component {
constructor(props) {
...
this.handleTabClick = this.handleTabClick.bind(this);
this.updateTabs = this.updateTabs.bind(this);
}
updateTabs(id){
let tabs = this.state.tabs;
let newtabs = tabs.map((tab, index)=>{
if(tab.id == id){
if(tab.active == true){
tab.active = false;
}else{
tab.active = true;
}
}else{
tab.active = false;
}
return tab;
});
return newtabs;
}
handleTabClick(id){
this.setState({tabs: this.updateTabs(id)});
}
render() {
...
<div>
<h2>Accordion:</h2>
<div id="accordion">
{
tabsArray.map((tab,index)=>{
return <Tab key={index} id={tab.id} handleclick={this.handleTabClick} title={tab.title} content={tab.content} active={tab.active} />
})
}
</div>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment