question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

isDirty is not reset when defaultValues is filled

See original GitHub issue

Describe the bug When useForm is called without defaultValues, the formState.isDirty works as expected: when a form value is changed, isDirty=true, and when this form value is emptied, isDirty=false. However, if some defaultValues is provided: when a form value is changed, isDirty=true, and when this form value is set back to its initial value, there is still isDirty=true.

Codesandbox link https://codesandbox.io/s/react-hook-form-formstate-dirty-touched-submitted-forked-jnkvl?file=/src/index.js:196-205

  1. Set a in the Mobile number field => you will see that isDirty=true and dirtyFields will contain Mobile number
  2. Empty the Mobile number field => you will see that isDirty=true and dirtyFields will be empty

It works correctly when no default values are provided: https://codesandbox.io/s/7o2wrp86k6?file=/src/index.js

Expected behavior The isDirty behavior should be consistent whether some defaultValues are provided or not.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
kidrocacommented, Nov 30, 2020

It seems like a bug to me - isDirty is true but dirtyFields is empty {}

7reactions
lilyannhcommented, Jan 4, 2021

@bluebill1049 Just piggybacking onto this discussion since my problem is similar.

The deep compare causes false positives in isDirty if the defaultValues object is not “perfect.” In these situations isDirty will be set to true as soon as you change focus on the form, and dirtyFields is still empty.

In my case, I initialize defaultValues with a “row” from the database. For example:

{
  id: 1,
  first_name: "Bart",
  last_name: "Simpson",
  note: null
}

In a form with the fields for First Name, Last Name, and Note, isDirty will be true without changing anything because:

  • note was null in defaultValues, but (I think) it gets compared to an empty string from the form
  • id exists in defaultValues, but does not exist in the form

I understand the reasoning behind the deep compare, but you can see from the example here that it is also causing some real usability issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I override the defaultValues in useForm and maintain ...
I considered setValues but I want to use the isDirty function, which allows field validation and the value used to check whether the...
Read more >
useForm - reset - React Hook Form
When defaultValues is not supplied to reset API, then HTML native reset API will be invoked to restore the form. Avoid calling reset...
Read more >
React Hook Form - Reset form with default values and clear ...
This is a quick example of how to reset a React Hook Form with default values and clear form validation error messages.
Read more >
Uncontrolled Components - React
Changing the value of defaultValue attribute after a component has mounted will not cause any update of the value in the DOM. render()...
Read more >
useForm - React Cool Form - Netlify
The defaultValues also used to compare against the current values to calculate isDirty and dirty . The defaultValues is cached at the first...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found