i am new to react and was making a basic counter. Logic might not be perfect but for now i want my counter to increase until the input value. Currently it decreases until 0 and increases unlimited. how can implement or bind my onChange function to onIncrease function?
import React from 'react';
import './App.css';
class App extends React.Component{
constructor(){
super();
this.state = {
counter: 0,
inputText: '',
}
}
// TODO: i should be able to increase only up to given input number
onChange = (event) => {
let inputText = event.target.value;
this.setState({inputText});
}
onIncrease = () => {
let counter = this.state.counter +1;
this.setState({counter})
}
onDecrease = () => {
if (this.state.counter > 0){
let counter = this.state.counter -1;
this.setState({counter})
}
}
render() {
console.log(this.state.counter);
return (
<div className="App">
<h2 id='s'>Counter: {this.state.counter}</h2>
<input onChange= {this.onChange}/>
<button onClick={this.onIncrease}>Increase</button>
<button onClick={this.onDecrease}>Decrease</button>
</div>
)
}
}
export default App;