Created
July 19, 2022 13:53
-
-
Save Blazing-Mike/c30addeeb781ea21d0d78a65d4529749 to your computer and use it in GitHub Desktop.
Implemented counter in react class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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