V
V
Vadim Sverdlik2019-09-27 15:25:43
React
Vadim Sverdlik, 2019-09-27 15:25:43

How to make the class on click added only to the element you click on?

I have an accordion on my site, I want that when a .card-header is clicked, the active class is added only to the .card-header that was clicked. Now implemented like this, it is added to all .card-header elements:

constructor(props) {
        super(props);
        this.state = {
            active: false
        }
    }

 toggle(){
        this.setState({active: !this.state.active});
}

<Accordion.Toggle as={Card.Header} eventKey={index} className={(this.state.active ? "active" : "")} onClick={e =>this.toggle()}>{item.name}</Accordion.Toggle>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question