reset: (values?: Record<string, any>, options?: Record<string, boolean>) => void
Reset the entire form state, you also have the ability to only reset specific parts of the state.
Rules
For controlled components like
React-Select
which do not expose aref
prop, you will have to reset the input value manually with setValue or hook your component with useController or Controller.You will need to pass
defaultValues
touseForm
in order toreset
theController
components' value.When you are subscribed/read the
formState
, it's important to decouplereset
withhandleSubmit
, both are updateformState
andhandleSubmit
is async by default. You can check out a working example below:When invoking
reset({ value })
without supplydefaultValues
atuseForm
, hook form will replacedefaultValues
with shallow clonevalue
object which you have supplied (not deepClone).// ❌ avoid the following with deep nested default values const defaultValues = { object: { deepNest: { file: new File() } } }; useForm({ defaultValues }); reset(defaultValues); // share the same reference // ✅ it's safer with the following, as we only doing shallow clone with defaultValues useForm({ deepNest: { file: new File() } }); reset({ deepNest: { file: new File() } });
Props
Reset
have the ability remain formState update, here are the options in detail:
Name | Type | Description |
---|---|---|
values | object | An optional object to reset from values. |
keepErrors | boolean | All errors will remain. This will not guarantee with further user actions. |
keepDirty | boolean |
|
keepValues | boolean | Form input values will be unchanged. |
keepDefaultValues | boolean | Keep the same defaultValues which is initialised at |
keepIsSubmitted | boolean |
|
keepTouched | boolean |
|
keepIsValid | boolean |
|
keepSubmitCount | boolean |
|
import React from "react"; import { useForm } from "react-hook-form"; export default function App() { const { register, handleSubmit, reset } = useForm(); const onSubmit = (data, e) => { // e.target.reset(); // HTML standard reset() function will only reset inputs' value }; useEffect(async () => { const result = await fetch('./api/formValues.json'); // result: { firstName: 'test', lastName: 'test2' } reset(result); // asynchronously reset your form values }, [reset]) return ( <form onSubmit={handleSubmit(onSubmit)}> <input {...register("firstName", { required: true })} /> <input {...register("lastName")} /> <input type="reset" /> // standard reset button <input type="button" onClick={() => reset({ firstName: "bill" }); }} /> // reset form with values <input type="button" onClick={() => { reset({ firstName: "bill" }, { keepErrors: true, keepDirty: true, keepIsSubmitted: false, keepTouched: false, keepIsValid: false, keepSubmitCount: false, }); }} /> </form> ); }
import React from "react"; 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> ); }