Skip to content

Instantly share code, notes, and snippets.

@nikolalsvk
Last active October 20, 2018 20:00
Show Gist options
  • Save nikolalsvk/c21394e45c9ef1a1ef8d9938c7924cdf to your computer and use it in GitHub Desktop.
Save nikolalsvk/c21394e45c9ef1a1ef8d9938c7924cdf to your computer and use it in GitHub Desktop.
import React, { Component } from "react";
class ButtonWithBind extends Component {
constructor() {
super();
this.state = { toggle: false };
this.toggleButton = this.toggleButton.bind(this);
}
toggleButton() {
this.setState(prevState => ({ toggle: !prevState.toggle }));
}
render() {
const toggle = this.state.toggle;
return (
<div>
<button onClick={this.toggleButton}>
{toggle ? "ON" : "OFF"}
</button>
</div>
);
}
}
export default ButtonWithBind;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment