Created
April 21, 2020 09:46
-
-
Save fobabs/499aaadfa6d8862c576cc73f7a7227bc to your computer and use it in GitHub Desktop.
A react on/off button component
This file contains 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 = { | |
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