Skip to content

Instantly share code, notes, and snippets.

@Manoz
Created August 12, 2018 09:42
Show Gist options
  • Save Manoz/2212b6e03dbe59528b41a57d3ec8af3a to your computer and use it in GitHub Desktop.
Save Manoz/2212b6e03dbe59528b41a57d3ec8af3a to your computer and use it in GitHub Desktop.
React active class on click
import React from 'react';
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { isToggledOn: false };
this.toggleClass = this.toggleClass.bind(this);
}
toggleClass() {
const currentState = this.state.isToggledOn;
this.setState({ isToggledOn: !currentState });
}
render() {
return (
<div>
<a
onClick={this.toggleClass}
className={this.state.isToggledOn ? 'some-class active' : 'some-class'}
href="http://www.example.com">My awesome link
</a>
</div>
);
}
}
export default MyComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment