I have a numeric input, which I want to validate with an onChange, but when said input is cleared, react throws ReferenceError: value is not defined which is understandable - it has nothing to validate.
I'm using this approach [Answer]
I'm using functional components and here is my code snippet:
const [formData, setFormData] = React.useState({
userTeamChosen: "",
eventNumber: 0,
value: 0,
ethValue: 0})
function handleNumeric(event) {
const { name, checkedValue, min, max } = event.target
value = Math.max(Number(min), Math.min(Number(max), Number(value)));
setFormData(prevFormData => {
return {...prevFormData,[name]: checkedValue}})}
<input
type="number"
name="value"
value={formData.value}
onChange={handleNumeric}
min="5"
max="10000000"/>
How should I change my validation, to keep in onChange, but without erroring, when the input is empty? Thanks in advance!