Form is dirty even though dirtyFields is empty

See original GitHub issue

Describe the bug Form is dirty when it should not be dirty

To Reproduce Steps to reproduce the behavior:

  1. Focus on first input
  2. Click elsewhere
  3. See isDirty: true

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-dirty-y7x03?file=/src/App.tsx

Expected behavior Expected isDirty to be false since nothing is changed and dirtyFields is empty

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:24 (18 by maintainers)

github_iconTop GitHub Comments

5reactions
rygo11commented, Aug 20, 2021

I had the same issue, so instead of using isDirty, I’m checking if the dirtyFields object is empty:

export const isEmpty = (obj: object) => {
  return Object.keys(obj).length === 0;
}

if(!isEmpty(dirtyFields)) { "insert code here" }

Seems to be working fine

4reactions
bluebill1049commented, Apr 15, 2021

You can build ur own 😃 getValues() and do ur own compare šŸ‘Œ

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 ...
Shouldn't isDirty be set back to false when the form is back to its initial state and dirtyFields should be empty? Is it...
Read more >
Remove Dirty Flag When Cell Value Is Changed to Original
An example on how to remove the dirty indicator when the value is put ... If the dirtyFields object is empty, set the...
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`
dirtyFields. { [string]: boolean }. An object full of booleans, with a value of true for each dirty field. Pristine fields will not...
Read more >
API Reference - Formik
<Formik> is a component that helps you with building forms. ... dirty: boolean ... Even if your form is empty by default, you...
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