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: reset() resets dirty values even though keepDirtyValues is set to true #11514

Closed
1 task done
bastibuck opened this issue Feb 15, 2024 · 4 comments
Closed
1 task done
Labels
status: under investigation aware of this issue and pending for investigation

Comments

@bastibuck
Copy link

bastibuck commented Feb 15, 2024

Version Number

7.50.1

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/condescending-forest-8wk8v2?file=%2Fsrc%2FApp.tsx%3A15%2C10

Steps to reproduce

  1. Go to the provided codesandbox example of a simple form that gets reset after some timeout (simulating a lazy load from an API)
  2. Fill in a firstName before the 5s timeout
  3. Watch the outputs of the dirtyFields, isDirty and current form values
  4. After the timeout and the triggered reset() the firstName is reset even though keepDirtyValues: true is set (and the dirtyFields is also subscribed by destructuring it)

Expected behaviour

The firstName field value should not be reset

What browsers are you seeing the problem on?

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bastibuck
Copy link
Author

It is totally possible that I am missing something. About the needed subscription of dirtyFields I read another issue #8341 which makes sense but doesn't solve my problem.

So happy to hear that I am doing something wrong but it might be bug as well.

@bastibuck
Copy link
Author

I also tried using resetField() in my current project to achieve the desired behaviour: load some data from an API and set this value as a new defaultState of my form for a certain field.

This also doesn't seem to work like I would read it from the docs https://react-hook-form.com/docs/useform/resetfield

I provided the new defaultValue for the field but it doesn't set neither the default nor the value.

I'm trying to set an array of objects, if that makes a difference

@bluebill1049
Copy link
Member

i just tried your demo, keepDirtyValues should be it and it's working in your link as well. let me know what's the issue and I will take a look at it agian.

@bluebill1049 bluebill1049 added the status: under investigation aware of this issue and pending for investigation label Feb 16, 2024
@bastibuck
Copy link
Author

After the reset the input UI still shows the correct value. But the firstName in formValues is reset to it's default of "" even though keepDirtyValues is set.

Make sure to enter a firstName before the 5s timeout or reload the demo browser in case you passed the timeout.

Look at Form values rendering and "firstName" especially.

Initial state

"firstName": ""

After manual update of input field

"firstName": "Tyler"

After fake loading timeout

"firstName": ""

(Other fields are correctly set to new default values)

bluebill1049 added a commit that referenced this issue Feb 24, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2024
rafaelcalhau pushed a commit to rafaelcalhau/react-hook-form that referenced this issue May 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: under investigation aware of this issue and pending for investigation
Projects
None yet
Development

No branches or pull requests

2 participants