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.

V7: formState.isDirty is not set to true onChange if value is watched

See original GitHub issue

Describe the bug In v7 in a form with only one input the formState.isDirty will not be true when the input is changed if you are watching the form value. It will become true once the input is blurred, however this is not good if the save button is disabled when !form.isDirty as it prevents the user from submitting the form unless they have blurred the input.

If you remove the watch on the form value it works as expected

To Reproduce Create a form with a single input and set the submit button to be disabled if !formState.isDirty

  1. Watch a field with something similar to what is below or in the codeSandbox
function Form(){
  const { register, formState, watch } = useForm()
  const { isDirty } = formState

  const name = watch('name')
  return <form>
    <input type='text' {...register('name')} />
    <button type='submit' disabled={!isDirty}>Save</button>
  </form>
}
  1. Change the field value
  2. The submit button will be disabled when it should be enabled

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-useform-template-forked-rpm8c?file=/src/index.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
tombburnellcommented, Mar 16, 2022

You want to set the shouldDirty flag to true in the third options argument of setValue.

You can learn more at RHF setValue method docs

setValue('name', 'value', { shouldDirty: true })

Brilliant thanks, that works! thanks for the pointer in the docs too.

4reactions
Moshyfawncommented, Mar 16, 2022

You want to set the shouldDirty flag to true in the third options argument of setValue.

You can learn more at RHF setValue method docs

setValue('name', 'value', { shouldDirty: true })
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 you change back to the original value, isDirty remains true, because ALL of the default values don't match the form input values...
Read more >
useForm - setValue - React Hook Form
This function allows you to dynamically set the value of a registered field and have ... Whether to compute if your entire form...
Read more >
Create powerful and flexible forms with React Hook Form
Learn how to create performant, flexible, and extensible forms with easy-to-use validation with React Hooks.
Read more >
React Hook Form - useForm: formState - YouTube
This session cover formState API inside react hook form.Doc: https://react-hook-form.com/api/useform/ formstate.
Read more >
Using Material UI with React Hook Form - LogRocket Blog
This tutorial is also helpful if you want to integrate some other ... value takes care of the actual value of the input,...
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