issue: There is no way to completely reset the form to its original state #10729
-
Version Number7.43.9 Codesandbox/Expo snackhttps://codesandbox.io/s/react-hook-form-reset-bug-2ycpw5?file=/src/index.tsx Steps to reproduce
Expected behaviourIn react-hook-form there is a method called 'reset'. It is meant to fully reset a form to its initial state. There are even some configuration options, like 'keepIsSubmitted' and others. However, this method doesn't completely reset the form and this form continues to be submitted. As a result, 'submit count' stays as '1' and 'reValidateMode' is used to validate fields instead of the regular 'mode'. But I want the form to behave as if it has never been submitted. What browsers are you seeing the problem on?Firefox, Chrome, Safari, Edge Relevant log outputNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I found the solution! This is not a bug, there is a correct solution. Only the 4th solution I've tried actually works out. Here is a codesandbox link: https://codesandbox.io/s/react-hook-form-reset-bug-resolved-lfp4w3?file=/src/index.tsx. In a nutshell, you should use const form = useForm();
const {
// ...
reset,
formState: { isSubmitSuccessful }
} = form;
React.useEffect(() => {
if (isSubmitSuccessful) {
reset();
}
}, [isSubmitSuccessful, reset]); |
Beta Was this translation helpful? Give feedback.
I found the solution! This is not a bug, there is a correct solution. Only the 4th solution I've tried actually works out. Here is a codesandbox link: https://codesandbox.io/s/react-hook-form-reset-bug-resolved-lfp4w3?file=/src/index.tsx.
In a nutshell, you should use
useEffect
+isSubmitSuccessful
formState flag to completely reset the form: