I have a <Sidebar /> component which creates two <TabLabel /> components.
I want to toggle the className is-active, if you click a tab label the class is-active should be added to the <li> element and removed on the other one.
There is no error ocuring, but the function toggleClass() doesn't seem to get invoked.
<Sidebar /> Component:
constructor() {
super();
this.state = {
active: 'generator'
}
this.toggleClass = this.toggleClass.bind(this);
}
toggleClass() {
this.setState({active: 'code'});
}
render() {
return (
<div className="sidebar">
<ul className="tabs-list">
<TabLabel onClick={() => this.toggleClass()} active={this.state.active} text="Generator" />
<TabLabel onClick={() => this.toggleClass()} active={this.state.active} text="Code" />
</ul>
</div>
);
}
<TabLabel /> Component:
render() {
return(
<li className={this.props.text.toLowerCase() === this.props.active ? "is-active" : null}>
<h2>{this.props.text}</h2>
</li>
);
}
console.log()in thetoggleClass()function to check whether it gets called?TabLabelaccept anonClickproperty? Does it do something with it?onClickwill be just the part ofprops, you need to use it insideTabLabelcomponent.toggleClass()in the<Sidebar />Component.