Created
January 10, 2020 06:09
-
-
Save diomededavid/a23baf6cfbf7a7abfe3f841c8a312d16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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