Q
Q
qfrontend2019-02-18 18:48:18
React
qfrontend, 2019-02-18 18:48:18

How to make clicked button active and others not active in React/Redux?

Greetings) There are several buttons on the page. How to make the button active when clicking on it (i.e. add the "active" class to it ). And make other buttons inactive (ie, remove their "active" class )? Through setState , when pressed, the "active" class is added , but how can I remove it from previously pressed buttons? Maybe through Redux like that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Filippov, 2019-02-18
@qfrontend

state = {
  activeBtnId = 'button1'
}

....

<button onClick={() => this.setState({activeBtnId: 'button2'})} className={this.state.activeBtnId === 'button2' ? 'active' : null}/>

Essence ^
The code can be combed and optimized

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question