Skip to content
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

Closed
1 task done
alexandresoro opened this issue Jun 10, 2023 · 3 comments
Closed
1 task done

issue: form not reset with values and keepDefaultValues option #10523

alexandresoro opened this issue Jun 10, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@alexandresoro
Copy link

alexandresoro commented Jun 10, 2023

Version Number

7.44.3

Codesandbox/Expo snack

https://codesandbox.io/s/billowing-water-kgvz44?file=/src/App.tsx

Steps to reproduce

  1. Go to CodeSandbox
  2. Click on 'set "values" first name to 'bob'' -> name is set to "bob" and isDirty should be true, which is OK
  3. Click on 'set "values" first name to 'mike'' -> name is set to "mike" and isDirty should be true, which is OK
  4. Then afterwards, click on 'set "values" first name to 'john'' -> name is not updated and isDirty 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", and isDirty should have been set to false accordingly.

Expected behaviour

The linked CodeSandbox is a simplified use case where I tried to achieve the following:

  • defaultValues is provided on useForm and is exactly the defaults for my form.
  • values is also provided fully and can be updated from outside react-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 expect defaultValues 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 to defaultValues, no reset occur, while I expected to have a regular reset that would recompute isDirty and field values, like for any other value.

From what I understand, this is because when values are updated, a reset is performed unless these new values deeply equal the default values:

if (props.values && !deepEqual(props.values, control._defaultValues)) {

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 to defaultValues 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 that isDirty and dirtyFields 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

  • I agree to follow this project's Code of Conduct
@bluebill1049
Copy link
Member

Click once again on 'update first name' -> no reset happens

what do you mean by this? what's your expected behavior?

@bluebill1049 bluebill1049 added the status: under investigation aware of this issue and pending for investigation label Jun 10, 2023
@alexandresoro
Copy link
Author

alexandresoro commented Jun 10, 2023

Click once again on 'update first name' -> no reset happens

what do you mean by this? what's your expected behavior?

Hi, I've updated the code to make it more explicit.
Basically, the buttons are there to simulate an external update of values.
When values gets updated, it is expected that the form gets updated accordingly.

When values is set to something that matches defaultValues and keepDefaultValues is true, I expected values and form state to be updated accordingly, which is not the case.

Since it works fine when setting anything else than the defaultsValues or when keepDefaultValues is not true, I suspect the behavior is inconsistent, but can be explained by the code I linked in my initial comment.

Hope it clarifies the use case 🙂

@bluebill1049 bluebill1049 added bug Something isn't working and removed status: under investigation aware of this issue and pending for investigation labels Jun 10, 2023
@bluebill1049
Copy link
Member

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants