Inconsistent behaviour for defaultValues and reset #11948
Unanswered
Veronika11111
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I have here weird behaviour with setting up default values and resetting form.
The problem started when we were not able to delete value from InputField, when we called onChange with undefined - the value fallbacked to the default value. So we investigated that and we found out that we should not use undefined, because when onChange is called with undefind it fallbacks to default value ... okay.
But then, we noticed that in some cases it is possible to call onChange with undefined and the input is cleared, even if the defaultValues are set.
We prepared the codepen with reproduction of these 2 cases: code sandbox
In both cases we use
useForm
with no default values and we set default values withreset
in useEffect.Then we log the default fields from
InputFirstName.tsx
Case 1
Button with modal in one component.
formState.defaultValues
and also for formState.defaultValues from useController fieldCase 2
Button is parent component with modal as child component. Whole form logic is in the child component
We know that using undefined is not recommended in react-hook-forms, but we would like to understand what is happening here and why the library behaves differently.
Beta Was this translation helpful? Give feedback.
All reactions