Trying to fill in some form fields based on a input of another form field when this is valid. Fairly new to react hook forms, so perhaps I'm looking at this all wrong. Issue I'm facing is that the onchange is triggered sooner than the validation.
const {
handleSubmit,
formState: { errors},
} = useForm<FormValues>({
mode: 'onChange'
});
const getAddress = (e) => {
if (errors.postalCode !== undefined) {
return;
}
//do stuff
};
<input
type={'text'}
{...register('postalCode', {
onChange: (e) => {
getAddress(e);
},
})}
/>
"react-hook-form": "^7.33.1"
So when the onChange is triggered, the errors is empty, so it continues with invalid data.
How can I make sure the validation is done before the onChange event.