To reset the isDirty
state without interfering with the form's values or default values, one example would be (being explicit):
reset(undefined, {keepValues: true, keepDirty: false, keepDefaultValues: false});
Example with React.useEffect()
hook:
I want to keep the submitted name
and filter
values and the ability to reset to the original defaultValues
, while resetting isDirty
:
const { reset, formState: { isSubmitSuccessful } } = useForm({
defaultValues: {
filter: "",
category: "",
}
});
React.useEffect(() => {
if(isSubmitSuccessful) {
reset(undefined, {keepValues: true, keepDirty: false, keepDefaultValues: false});
}
}, [isSubmitSuccessful, reset]);
The useForm Documentation explains under props the different options available for reset()
. They can also be referenced in the project's TypeScript Declaration
Infered type:
const reset: (values?: any, keepStateOptions?: Partial<{
keepDirtyValues: boolean;
keepErrors: boolean;
keepDirty: boolean;
keepValues: boolean;
keepDefaultValues: boolean;
keepIsSubmitted: boolean;
keepTouched: boolean;
keepIsValid: boolean;
keepSubmitCount: boolean;
}> | undefined) => void