reset: <T>(values?: T | ResetAction<T>, options?: Record<string, boolean>) => void
Reset the entire form state, fields reference, and subscriptions. There are optional arguments and will allow partial form state reset.
Props
Reset has the ability to retain formState. Here are the options you may use:
| Name | Type | Description |
|---|---|---|
values | object | An optional object to reset form values, and it's recommended to provide the entire defaultValues when supplied. |
keepErrors | boolean | All errors will remain. This will not guarantee with further user actions. |
keepDirty | boolean |
Important: this keep option doesn't reflect form input values but only dirty fields form state. |
keepDirtyValues | boolean |
Important: formState |
keepValues | boolean | Form input values will be unchanged. |
keepDefaultValues | boolean | Keep the same defaultValues which are initialised via
|
keepIsSubmitted | boolean |
|
keepTouched | boolean |
|
keepIsValid | boolean |
|
keepSubmitCount | boolean |
|
Rules
For controlled components you will need to pass
defaultValuestouseFormin order toresettheControllercomponents' value.When
defaultValuesis not supplied toresetAPI, then HTML native reset API will be invoked to restore the form.Avoid calling
resetbeforeuseForm'suseEffectis invoked, this is becauseuseForm's subscription needs to be ready beforeresetcan send a signal to flush form state update.It's recommended to
resetinsideuseEffectafter submission.useEffect(() => { reset({ data: 'test' }) }, [isSubmitSuccessful])
Examples
import React, { useCallback } from "react"; import { useForm } from "react-hook-form"; export default function App() { const { register, handleSubmit, reset } = useForm(); const resetAsyncForm = useCallback(async () => { const result = await fetch('./api/formValues.json'); // result: { firstName: 'test', lastName: 'test2' } reset(result); // asynchronously reset your form values }, [reset]); useEffect(() => { resetAsyncForm() }, [resetForm]) return ( <form onSubmit={handleSubmit((data) => {})}> <input {...register("firstName")} /> <input {...register("lastName")} /> <input type="button" onClick={() => { reset({ firstName: "bill" }, { keepErrors: true, keepDirty: true, }); }} /> <button onClick={() => { reset(formValues => ({ ...formValues, lastName: 'test', })) }} > Reset partial </button> </form> ); }import { useForm } from "react-hook-form"; interface UseFormInputs { firstName: string lastName: string } export default function Form() { const { register, handleSubmit, reset, formState: { errors } } = useForm<UseFormInputs>(); const onSubmit = (data: UseFormInputs) => { console.log(data) }; return ( <form onSubmit={handleSubmit(onSubmit)}> <label>First name</label> <input {...register("firstName", { required: true })} /> <label>Last name</label> <input {...register("lastName")} /> <input type="submit" /> <input type="reset" value="Standard Reset Field Values" /> <input type="button" onClick={() => reset()} value="Custom Reset Field Values & Errors" /> </form> ); }
Video
The following video explains in detail each different formState represents and functionality within the reset API.
Thank you for your support
If you find React Hook Form to be useful in your project, please consider to star and support it.