Skip to content

Instantly share code, notes, and snippets.

@ovpv

ovpv/accordion.js Secret

Created Mar 23, 2019
Embed
What would you like to do?
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