-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue: form not reset with values and keepDefaultValues option #10523
Comments
what do you mean by this? what's your expected behavior? |
Hi, I've updated the code to make it more explicit. When Since it works fine when setting anything else than the Hope it clarifies the use case 🙂 |
Thanks for the detailed issue report and explanation even consider if this is the intended behavior to update the doc. Sometimes, I wish every bug report have this sort of quality and understanding before being sent. You even look into the code base to find potential issues which helps me a lot over the weekend to try to maintain this library. 🙏 Thank you. |
Version Number
7.44.3
Codesandbox/Expo snack
https://codesandbox.io/s/billowing-water-kgvz44?file=/src/App.tsx
Steps to reproduce
isDirty
should be true, which is OKisDirty
should be true, which is OKisDirty
is not updated either, which is not OK. What is expected is the same as for other values: name should have been set to "john", andisDirty
should have been set tofalse
accordingly.Expected behaviour
The linked CodeSandbox is a simplified use case where I tried to achieve the following:
defaultValues
is provided onuseForm
and is exactly the defaults for my form.values
is also provided fully and can be updated from outsidereact-hook-form
. In my case, this is via a micro-state library as some fields can be updated from a component outside the form (e.g. moving a cursor on a map that updates coordinates).resetOptions
is set to{ keepDefaultValues: true }
. The rationale is that, I expectdefaultValues
to be the source of truth to determine if the form is dirty and which fields are dirty.When setting
values
to something that is deeply equal todefaultValues
, no reset occur, while I expected to have a regular reset that would recomputeisDirty
and field values, like for any other value.From what I understand, this is because when
values
are updated, areset
is performed unless these new values deeply equal the default values:react-hook-form/src/useForm.ts
Line 104 in 6d60040
In the general use case, it makes sense as it avoids performing a useless reset when both are "the same", but I feel that this is not true when one expects the default values not be changed with
keepDefaultValues
.I'm not sure of the impacts but I would expect to always perform a reset when
keepDefaultValues
as by definition, it won't make sense to compare the new values todefaultValues
here.Not sure if this is a bug or something intended, and maybe this is an edge case that could be documented as the current documentation of
keepDefaultValues
states thatisDirty
anddirtyFields
will be checked/updated again at least.What browsers are you seeing the problem on?
Firefox, Chrome
Relevant log output
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: