I'm struggling with implementing 'toggle class' functionality in React.
I have the NavSidebar component which is an unordered list ul. My NavListItem component is list item li.
When the list item is clicked I need to give the class name active to the clicked li and other list elements should get an empty class name.
Here's what I've come up with by far:
class NavListItem extends React.Component {
constructor(props) {
super(props);
this.state = { active: false };
}
setActive(bool) {
this.setState({ active: bool });
}
render() {
let index = this.props.key;
let item = this.props.item;
return (
<li className={this.state.active ? 'active' : ''}
onClick={this.setActive(this.props.setActive).bind(this)} >
<a href={'#'+item}>{item}</a>
</li>
);
}
}
class NavSidebar extends React.Component {
constructor(props) {
super(props);
}
render () {
let items = this.props.navItems.map( (item, index) => {
return (
<NavListItem key={index} item={item} setActive={true} />
);
});
return (
<div className="col-sm-2">
<nav className="nav-sidebar">
<ul className="nav">
{items}
<li className="nav-divider" />
<li>
<a href="#"><i className="glyphicon glyphicon-home"></i> Back Home</a>
</li>
</ul>
</nav>
</div>
);
}
}
NavSidebar.propTypes = {
navItems: React.PropTypes.array
};
Unfortunately, this doesn't work, it never even touches the className of the elements and I can't come up with the working solution.
Could you please help me with that?