I want to increment/decrement counter based on button click. Scenario: There is an initial value on the button- say: 5 When a user clicks on the button, it increments to +1 - o/p: 6 however when the user clicks again on the button it, it'll reduce to 5 again. this case I dont have 2 buttons- inc and dec to increase/decrease the count.
code:
class Hello extends React.Component {
constructor() {
super()
this.state = {
count: 10,
}
}
getCount( c ) {
const clicked = this.state.clicked
if(c == 1){
this.setState({count: this.state.count +1, clicked: true});
} else {
this.setState({count: this.state.count -1})
}
}
render() {
return <div>
<button onClick={this.getCount.bind(this, 1)}> click me for increase </button>
<button onClick={this.getCount.bind(this, 0)}> click me for decrease </button>
<div><h1>{this.state.count}</h1></div>
</div>;
}
}
ReactDOM.render(
<Hello name="World" />,
document.getElementById('root')
);