A
A
And19912018-12-19 23:27:09
React
And1991, 2018-12-19 23:27:09

How to add and remove a class when I click on an element?

How to add and remove a class when I click on an element?

<a href="/" onClick={this.showAllBook.bind(this)}>All Book</a>

and the handler hangs on it
showAllBook(event) {
    event.preventDefault();
    const name = "One";
    this.props.showAllBooks();

which inherits props from parent component
showAllBooks() {
    const { allBooks } = this.state;
    this.setState({filteredBooks: allBooks});
}

how can i add class to clickable link and remove when another one is clicked?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-12-20
@And1991

<a 
  className={filter === 'all' ? 'active' : null}
  onClick={() => setFilter('all')}
>
  All Book
</a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question