isDirty is not reset when defaultValues is filled
See original GitHub issueDescribe 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
- Set
a
in theMobile number
field => you will see thatisDirty=true
anddirtyFields
will containMobile number
- Empty the
Mobile number
field => you will see thatisDirty=true
anddirtyFields
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:
- Created 3 years ago
- Comments:12 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It seems like a bug to me -
isDirty
istrue
butdirtyFields
is empty{}
@bluebill1049 Just piggybacking onto this discussion since my problem is similar.
The deep compare causes false positives in
isDirty
if thedefaultValues
object is not “perfect.” In these situationsisDirty
will be set totrue
as soon as you change focus on the form, anddirtyFields
is still empty.In my case, I initialize
defaultValues
with a “row” from the database. For example:In a form with the fields for
First Name
,Last Name
, andNote
,isDirty
will betrue
without changing anything because:note
wasnull
in defaultValues, but (I think) it gets compared to an empty string from the formid
exists in defaultValues, but does not exist in the formI understand the reasoning behind the deep compare, but you can see from the example here that it is also causing some real usability issues.