Skip to content

Instantly share code, notes, and snippets.

@diomededavid
Created January 10, 2020 06:09
Show Gist options
  • Select an option

  • Save diomededavid/a23baf6cfbf7a7abfe3f841c8a312d16 to your computer and use it in GitHub Desktop.

Select an option

Save diomededavid/a23baf6cfbf7a7abfe3f841c8a312d16 to your computer and use it in GitHub Desktop.
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = {isToggleOn: true};
// This binding is necessary to make `this` work in the callback
this.handleClick = this.handleClick.bind(this);
}
true = 'Im On!';
false = 'Im Off!';
handleClick() {
this.setState(state => ({
isToggleOn: !state.isToggleOn
}));
}
render() {
return (
<button onClick={this.handleClick}>
{this.state.isToggleOn ? this.true : this.false}
</button>
);
}
}
render(
<Toggle />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment