Created
October 18, 2018 07:55
-
-
Save shsunmoonlee/bc9b608e773b66832ab2adee7ce674d3 to your computer and use it in GitHub Desktop.
React bind example
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); | |
| } | |
| 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