Skip to content

Instantly share code, notes, and snippets.

@dnshko
Created July 31, 2020 10:41
Show Gist options
  • Save dnshko/b4c3b735792b18d6a9723c35b1affb3e to your computer and use it in GitHub Desktop.
Save dnshko/b4c3b735792b18d6a9723c35b1affb3e to your computer and use it in GitHub Desktop.
REACT CLASS COMPONENT SYNTAX
class Counter extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0,
};
this.onIncrement = this.onIncrement.bind(this);
this.onDecrement = this.onDecrement.bind(this);
}
onIncrement() {
this.setState(state => ({ counter: state.counter + 1 }));
}
onDecrement() {
this.setState(state => ({ counter: state.counter - 1 }));
}
render() {
return (
<div>
<p>{this.state.counter}</p>
<button
onClick={this.onIncrement}
type="button"
>
Increment
</button>
<button
onClick={this.onDecrement}
type="button"
>
Decrement
</button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment