V7: formState.isDirty is not set to true onChange if value is watched
See original GitHub issueDescribe 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
- 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>
}
- Change the field value
- 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:
- Created 2 years ago
- Comments:15 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Brilliant thanks, that works! thanks for the pointer in the docs too.
You want to set the
shouldDirty
flag totrue
in the third options argument ofsetValue
.You can learn more at RHF
setValue
method docs