I want to check in a RHF if the user has modified the form values with the isDirty parameter.
Setting the defaultValues manually works:
const {handleSubmit, control, setValue, setError, setFocus} = useForm({defaultValues: {name: ""}});
This seem to work correctly.
But when the user tries to edit a form I load the values into the form with setValue.
Now I don't know how I can set programatically the defaultValues.
How to change React-Hook-Form defaultValue with useEffect()?
This is how I do, but the answer is not really correct. The values are set, but the defaultValues don't change this way so RHF can't compare.
Make sure to provide all inputs' defaultValues at the useForm, so hook form can have a single source of truth to compare whether the form is dirty.
https://react-hook-form.com/api/useform/formstate
How can I set the defaultValues dynamically so it even works in 'edit' mode?
reset({...defaultValues})?setValueor how?setValuein order to set thedefaultValue. Make an object ofdefaultValueinuseEffect. Define all the neccasory fields like in your casename. Do something likedefaultValue.name = 'Kristof'and at last doreset({...defaultVales}). Also checkout `reset`` for more clarification. react-hook-form.com/api/useform/reset