I have input whose value is changed by button element. I need a way to call onChange
of that input. I have some validation that has to be done when the value is changed.
I found some answers on StackOverflow but they are using dispatch which they didn't explain properly or I didn't use it properly.
But onChange
is not called when the state is updated. Here is what I have by now
<div className="input-group">
<input type="text" className="form-control quantity-field text-center" onChange={(e) => this.handleChange(e)} value={this.state.quantity}/>
<button onClick={(e) => this.handleIncDec(e)}>+</button>
</div>
handleIncDec = (e) => {
e.preventDefault()
this.setState({
quantity: this.state.quantity += 1
})
}
handleChange = e => {
console.log(e.target.value);
const re = /^[0-9\b]+$/;
if (e.target.value === '' || re.test(e.target.value)) {
this.setState({
quantity: e.target.value
})
}
};
The value of the input is updated correctly as it should be but onChange is never called unless I updated value directly in that input and not with button, which is not what I want.
If you need any other info to tell me and I will provide it.