Files
codeql/javascript/ql/src/Expressions/examples/UnboundEventHandlerReceiver_fixed.js
2018-08-02 17:53:23 +01:00

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>
);
}
}