-
Maybe there's an obvious reason I'm not considering, but I had a situation like the following: const someApiCallThatFails = () => new Promise((resolve, reject) => setTimeout(reject, 1000));
const { handleSubmit, formState: { isSubmitting } } = useForm();
const onSubmit = handleSubmit(async (values) => {
await someApiCallThatFailed(values);
});
return (
<form onSubmit={onSubmit}>
<button type="submit" disabled={isSubmitting}>Submit</button>
</form>
) The promise ends up rejecting, but It works as expected by wrapping the await in a try-catch or appending |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
RHF doesn't support throwing errors in the submit handler with the current API design. We used to "swallow" the error, which is wrong. When you throw inside the submit handler, some part of RHF state handling code is not run, hence your use-case. Please, handle rejected promises in your code instead. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue with hook form(v7.43.0) and wrapping the function inside try&catch block didn't work, const { isLoading, mutateAsync } = useMutation({
mutationFn: asyncFunc
//...config
});
const { handleSubmit } = useForm({
//...config
});
return (
<form onSubmit={handleSubmit(mutateAsync)}>
<button type='submit' disabled={isLoading}>Submit</button>
</form>
); |
Beta Was this translation helpful? Give feedback.
RHF doesn't support throwing errors in the submit handler with the current API design.
We used to "swallow" the error, which is wrong.
When you throw inside the submit handler, some part of RHF state handling code is not run, hence your use-case.
Please, handle rejected promises in your code instead.