Maybe, it will be helpful for someone
Recently I used this solution for my App
I am not sure that is a correct solution but it works fine.
this.state = {
inputValue: "",
isInputNotValid: false
}
handleInputValue = (evt) => {
this.validationField(evt, "isInputNotValid", "inputValue");
}
validationField = (evt, isFieldNotValid, fieldValue ) => {
if (evt.target.value && !isNaN(evt.target.value)) {
this.setState({
[isFieldNotValid]: false,
[fieldValue]: evt.target.value,
});
} else {
this.setState({
[isFieldNotValid]: true,
[fieldValue]: "",
});
}
}
<input className={this.state.isInputNotValid ? "error" : null} type="text" onChange="this.handleInputValue" />
The main idea, that state won't update till the condition isn't true and value will be empty.
Don't need to use onKeyPress, Down etc.,
also if you use these methods they aren't working on touch devices