I have research this for quite a while, but was not able to find a solution. I have a fontawesome arrow toggle in couple of buttons, and I am trying to get them to toggle. the toggle is working, but they are all toggling at the same time. How can I isolate it to the clicked component only?
My example is massive, but I added these buttons, which pretty much has the same functionality ... so in this example, I would like just the clicked button to switch the start/stop text.
class Button extends React.Component {
constructor() {
super();
this.toggleState = this.toggleState.bind(this);
this.state = {
isActive : false
}
}
toggleState() {
this.setState({isActive:!this.state.isActive});
}
render() {
return(
<div>
<button className={this.state.isActive ? 'active' : 'inactive'} onClick ={this.toggleState}>{this.state.isActive ? 'STOP' : 'START'}</button>
<button className={this.state.isActive ? 'active' : 'inactive'} onClick ={this.toggleState}>{this.state.isActive ? 'STOP' : 'START'}</button>
<button className={this.state.isActive ? 'active' : 'inactive'} onClick ={this.toggleState}>{this.state.isActive ? 'STOP' : 'START'}</button>
</div>
);
}
}
ReactDOM.render(<Button />,document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
Buttonelement in your React tree, and there's only exactly one variable storing the state. Then you add threebuttonelements that all do the exact same thing: toggle the singleButton's state.