React Hook Form isDirty does not go back to false
formState.isDirty is still true after the user types the original value backShort 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.
Fix it fast
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 });2Match what you see, then jump to its fix
- 1
Typing then clearing a field leaves isDirty trueA field has no default valueFix - 2
isDirty compares against data you loaded, not the original defaultsreset(values) or the values prop moved the baselineFix - 3
Values change in code but isDirty stays falsesetValue was called without the dirty optionFix - 4
isDirty never changes in the component that reads itformState was read in a way that skips the subscriptionFix
- 1
What does the error look like?
formState.isDirty is still true after the user types the original value backformState.isDirty is false after setValuedirtyFields 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.tsx2Fix
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); // falseConfirmed by react-hook-form.com
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
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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
The validation message never appears under an MUI X Date Picker
The field never turns red after a failed validation
A DatePicker does not take helperText or error as direct props.
3 causes, each with a fixWeb apps
React Hook Form: the submit button does nothing and onSubmit never runs
The page reloads when the form is submitted
handleSubmit runs your onSubmit only when validation passes.
4 causes, each with a fixWeb apps
react-hook-form: an optional number input gives NaN instead of a number or null
Validation fails on an optional numeric field
valueAsNumber returns NaN when a value cannot be converted, and setValueAs is ignored while valueAsNumber is on.
3 causes, each with a fixWeb apps
A Zod refinement error does not appear or does not clear in React Hook Form
The refinement error never shows until every other field is valid
Three Zod and React Hook Form rules explain it.
4 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fix