I have the following structure in HTML/JSX
<ul>
<li className="active">Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
I want to add class name "active" on the <li> element I click on. For example: clicking on tab 2 should remove class "active" from tab1 and add it to tab2.
Pasting my react code here for better understanding.
import React, {
Component
} from 'react';
export default class Tab extends Component {
constructor(props){
super(props);
this.changeTab = this.changeTab.bind(this);
}
changeTab(){
// solution here
}
render() {
return (
<div>
<ul>
<li onClick={this.changeTab}>Tab 1</li>
<li onClick={this.changeTab}>Tab 2</li>
<li onClick={this.changeTab}>Tab 3</li>
</ul>
</div>
);
}
}
How to achieve this functionality using the React way? I don't want to use any other external library.TIA.