I'm fairly new to React and struggle to find a suitable answer to my question. I've got a list with multiple buttons (23) and want to add an .active class to every individual button that has been clicked. I am saving the buttons "value" in a a state array for later usage. Ergo, there can be multiple buttons with an active class and no buttons with the respective class.
class DistrictModal extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedDistricts: []
}
}
addDistrictToList = (e) => {
this.setState(prevState => ({
selectedDistricts: [...prevState.selectedDistricts, e]
}));
}
render() {
<div className="list-group">
<button type="button" onClick={() => this.addDistrictToList("1")} > 1. bezirk </button>
<button type="button" onClick={() => this.addDistrictToList("2")} > 2. bezirk </button>
<button type="button" onClick={() => this.addDistrictToList("3")} > 3. bezirk </button>
</div>
}}