Skip to content

Instantly share code, notes, and snippets.

@fobabs
Created April 21, 2020 09:46
Show Gist options
  • Save fobabs/499aaadfa6d8862c576cc73f7a7227bc to your computer and use it in GitHub Desktop.
Save fobabs/499aaadfa6d8862c576cc73f7a7227bc to your computer and use it in GitHub Desktop.
A react on/off button component
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {
isTToggleOn: true
};
// binding the handleClick method
this.handleClick = this.handleclick.bind(this);
}
handleClick() {
this.setState(state => ({
isToggleOn: !state.isToggleOn
}));
}
render() {
return(
<button onClick={this.handleClick}>
{this.state.isToggleOn ? 'ON' : 'OFF'}
</button>
);
}
}
ReactDOM.render(
<Toggle />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment