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 changing to true when no field is actually dirty

See original GitHub issue

Right 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.

https://codesandbox.io/s/react-hook-form-formstate-dirty-touched-submitted-forked-7jn4k?file=/src/index.js

This was not the case with 6.7.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:39 (10 by maintainers)

github_iconTop GitHub Comments

20reactions
vvocommented, Dec 22, 2020

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.

17reactions
vvocommented, Feb 4, 2021

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:

const numberValue = 10;

const form = useForm({
  defaultValues: {
    numberField: numberValue
  }
});

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 a string, you might want to double check that”.

WDYT?

Thanks!

Read more comments on GitHub >

github_iconTop 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 >

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