Skip to content

Instantly share code, notes, and snippets.

@Blazing-Mike
Created July 19, 2022 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blazing-Mike/c30addeeb781ea21d0d78a65d4529749 to your computer and use it in GitHub Desktop.
Save Blazing-Mike/c30addeeb781ea21d0d78a65d4529749 to your computer and use it in GitHub Desktop.
Implemented counter in react class
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
this.decrement = this.decrement.bind(this);
this.increment = this.increment.bind(this);
this.reset = this.reset.bind(this);
}
increment(){
this.setState(state => ({
count: state.count + 1
}))
}
decrement(){
this.setState(state => ({
count : state.count - 1
}))
}
reset(){
this.setState(({
count : 0
}))
}
render() {
return (
<div>
<button className='inc' onClick={this.increment}>Increment!</button>
<button className='dec' onClick={this.decrement}>Decrement!</button>
<button className='reset' onClick={this.reset}>Reset</button>
<h1>Current Count: {this.state.count}</h1>
</div>
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment