Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React Hook Form isDirty does not go back to false

ErrorformState.isDirty is still true after the user types the original value back

Short answer

React Hook Form sets isDirty by comparing the current values with defaultValues. If isDirty looks wrong, the comparison baseline is wrong: a field has no default, reset replaced the defaults, setValue never asked for dirty tracking, or formState was not read during render. Fix the baseline first.

  • Applies to react-hook-form 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the baseline and the live state side by side. If defaultValues is missing a field or differs from what you expect, you have found the cause.

    const { formState: { isDirty, dirtyFields, defaultValues } } = useForm({ defaultValues });
    console.log({ isDirty, dirtyFields, defaultValues });
  2. 2Match what you see, then jump to its fix

    1. 1Typing then clearing a field leaves isDirty trueA field has no default valueFix
    2. 2isDirty compares against data you loaded, not the original defaultsreset(values) or the values prop moved the baselineFix
    3. 3Values change in code but isDirty stays falsesetValue was called without the dirty optionFix
    4. 4isDirty never changes in the component that reads itformState was read in a way that skips the subscriptionFix

What does the error look like?

  • formState.isDirty is still true after the user types the original value back
  • formState.isDirty is false after setValue
  • dirtyFields is an empty object while the form values changed

Cause 1 of 4

A field has no default value

1Check

A field without a default starts as undefined. Once the user types and deletes the text, its value is an empty string, which is no longer equal to the default.

grep -n "useForm(" -A8 src/ProfileForm.tsx

2Fix

Give every input a default in useForm, an empty string included.

useForm({
  defaultValues: { name: "", email: "", bio: "" },
});

3Verify

Type into a field, clear it again, and read isDirty.

console.log(formState.isDirty); // false

Confirmed by react-hook-form.com

Did this fix it?

Cause 2 of 4

reset(values) or the values prop moved the baseline

1Check

Values passed to reset become the new defaults, so a later bare reset() returns to those. If you expected the first defaults to stay the comparison point, this is the reason.

grep -n "reset(" -r src/

2Fix

Use keepDefaultValues when you want the original defaults to remain the baseline. When you want the saved data to be the new baseline, pass it to reset after saving.

reset(serverData, { keepDefaultValues: true }); // original baseline stays
// after a successful save, make the saved data the baseline:
reset(savedData);

3Verify

Change a field, change it back, and check isDirty.

console.log(getValues(), formState.defaultValues);

Confirmed by react-hook-form.com

Did this fix it?

Cause 3 of 4

setValue was called without the dirty option

1Check

setValue only changes the value by default. It does not mark the field dirty, touched or validate it unless you ask.

grep -n "setValue(" -r src/

2Fix

Pass shouldDirty when the change should count as a user edit.

setValue("email", "new@example.com", { shouldDirty: true });

3Verify

Call setValue and watch dirtyFields.email.

console.log(formState.dirtyFields);

Confirmed by react-hook-form.com

Did this fix it?

Cause 4 of 4

formState was read in a way that skips the subscription

1Check

formState is a Proxy: a property is tracked only if it is read during render. Reading it inside a condition, such as !formState.isDirty || !formState.isValid, can skip the second property.

grep -n "formState\." -r src/

2Fix

Destructure everything you need from formState at the top of the component, before any condition.

const { isDirty, isValid } = formState;
const canSave = isDirty && isValid;

3Verify

Edit a field and watch the button state update.

console.log(isDirty, isValid);

Confirmed by react-hook-form.com

Did this fix it?

How do I stop it coming back?

  • Always pass a complete defaultValues object, with empty strings instead of undefined.
  • Load server data through the values prop or reset(data), not through setValue calls.
  • Pass shouldDirty and shouldValidate to setValue whenever the change should behave like typing.
  • Destructure formState properties at the top of the component.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.