isDirty changing to true when no field is actually dirty
See original GitHub issueRight after I added:
defaultValues: {
"First name": "Name"
},
If you click on a field and then outside (forcing a blur) the isDirty flag will change to true, but the dirtyFields will not change.
This was not the case with 6.7.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:39 (10 by maintainers)
Top Results From Across the Web
React-hook-form doesn't set isDirty to false when back to initial ...
When the form is first rendered, isDirty will be false. When you change a value, isDirty will be true. When you change back...
Read more >useFormState - Simple React forms validation
Dirty fields will not represent as isDirty formState, because dirty fields are marked field dirty at field level rather the entire form.
Read more >Final Form Docs – `FormState`
An object full of booleans, with a value of true for each dirty field. ... true if the form values have ever been...
Read more >Detect Unsaved Changes in Angular Forms | by Netanel Basal
If the user changes the data and leave the page without saving, ... perform a deep equality check and return a boolean indicating...
Read more >React Hook Form - formState (Dirty, Touched, Submitted)
React Hook Form - formState (Dirty, Touched, Submitted). Form State: Dirty, isValid, Touched, isSubmitted, isSubmitting, SubmitCount. react-hooks.
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
Small note because I faced a similar issue @mackankowski, I had a defaultValues object that contained MORE properties than the actual fields that were registered through react-hook-form. Which triggered this weird state of isDirty: true while dirtyFields was {}.
I think it’s good that we enforce consistency here (fields in defaultValues must be the same as registered fields), but I wish it was more obvious that I made a mistake.
At this point I think I was born to discover edge cases. Welcome to another episode of “Dirty Or Not, Here I Come”.
Here’s another case of dirty going for true and never going back to false afterwards. Let’s say you’re building a form that as a number selector (
<input type="number"
) AND you pass it actual number values (to defaultValue and defaultValues) this way:You’ll get in trouble! Why? Because a number input value is actually … a string! So when react hook form compares “10” to 10 to check for dirty state it will say it’s dirty, while it’s not from your POV.
I wonder if I am doing something wrong OR not doing something right as for serialization between my form and my actual data types… Any idea? Should we do a bit of type checking between defaultValues and form values? So we could warn in console like “numberField defaultValue was a
number
but form says it’s astring
, you might want to double check that”.WDYT?
Thanks!