mirror of
https://github.com/github/codeql.git
synced 2026-01-31 15:22:57 +01:00
24 lines
539 B
JavaScript
24 lines
539 B
JavaScript
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(prevState => ({
|
|
isToggleOn: !prevState.isToggleOn
|
|
}));
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<button onClick={this.handleClick}> // GOOD, the constructor binds `handleClick`
|
|
{this.state.isToggleOn ? 'ON' : 'OFF'}
|
|
</button>
|
|
);
|
|
}
|
|
}
|