Capítulo 16 de 39
reset(values?, options?) resets the entire form — values, field refs, and subscriptions — with granular keepX flags to preserve specific pieces of state across the reset.
(currentValues) => nextValues (7.36.0+) to reset to.reset(values) updates defaultValues: unless keepDefaultValues is set — a later reset()/reset({}) with no values then resets to that last-provided values, not the original useForm defaults.keepDirty preserves dirtyFields/isDirty state itself (not input values); keepDirtyValues (7.31.0+) preserves dirty fields and their current values while non-dirty fields update to the new values — requires dirtyFields to be subscribed.useForm defaults instead of adopting the new values; isDirty/dirtyFields recompute against the new values vs. those original defaults.formState slice instead of clearing it.reset() inside a useEffect watching formState.isSubmitSuccessful, not directly inside the onSubmit callback — execution order matters.reset(result) // full async reset, e.g. after fetching saved values
reset({ firstName: "bill" }, { keepErrors: true, keepDirty: true }) // partial reset, preserve some state
reset((formValues) => ({ ...formValues, lastName: "test" })) // callback form
useEffect(() => {
if (formState.isSubmitSuccessful) {
reset({ something: "" })
}
}, [formState.isSubmitSuccessful, reset])
formState.isSubmitSuccessful in an effect rather than called synchronously inside onSubmit.| Option | Preserves |
|---|---|
keepErrors | error state (not guaranteed to survive further user action) |
keepDirty | dirty state flags (not input values) |
keepDirtyValues | dirty fields' state + values; only non-dirty fields adopt new values |
keepValues | current input values entirely |
keepDefaultValues | the original useForm defaultValues |
keepIsSubmitted / keepIsSubmitSuccessful / keepSubmitCount | submission-history flags |
keepTouched | isTouched |
keepIsValidating | in-flight validation state |
keepIsValid | isValid (temporarily, until next user action) |
keepFieldsRef | internal field refs (no unmount/remount) |
reset() synchronously inside onSubmit: execution order can bite you — gate it on formState.isSubmitSuccessful in a useEffect instead.keepDefaultValues/awareness that it becomes the new baseline: subsequent no-argument resets return to that partial set, not the original defaults.reset(values) becomes the new baseline for future resets unless keepDefaultValues is set.keepDirty and keepDirtyValues are different things — one preserves flags, the other preserves both flags and the actual dirty values.useEffect + isSubmitSuccessful pattern for reset-after-submit.isSubmitSuccessful flag driving the recommended reset pattern.reset with a values object containing array data replaces the whole array too.