I am trying to add class active when item in list is clicked. When second item is clicked, remove class from first item and add it to second one.
I have this code so far:
constructor(props) {
super(props)
this.state = {
clicked: false,
showComponent: "Box1"
};
}
addClassName = () => {
this.setState({
clicked: true
});
}
<ul>
<li className = {
this.state.clicked ? 'active' : 'inactive'
}
onClick = {() => {
this.toggleDiv("monday");
this.addClassName();
}
} data-sched = "monday"> Monday </li>
<li className = {
this.state.clicked ? 'active' : 'inactive'
}
onClick = {() => {
this.toggleDiv("thursday");
this.addClassName();
}
}
data-sched = "tuesday" > Thursday </li>
</ul>
The problem is when I click first item class active is applied on both (first and second item.)
stateneeds to hold which value has been clicked, not just that a value has been clicked. Also there is an obvious code duplication that you should solve.