reset
Reset the entire form state and fields references. There are optional arguments to allow partial form state reset.
<T>(values?: T, options?: Record<string, boolean>) => void
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 |
| opions.keepErrors | boolean | All errors will remain. This will not guarantee with further user actions. |
| options.keepDirty | boolean | Retain formState.dirtyFields(). |
| opions.keepValues | boolean | Form input values will be unchanged. |
| options.keepTouched | boolean | Retain formState.touchedFields(). |
| options.keepIsSubmitted | boolean | isSubmitted state will be unchanged. |
| options.keepSubmitCount | boolean | submitCount state will be unchanged. |
Uncontrolled example:
import { createForm } from "solid-hook-form"
export const ExampleForm = () => {
const form = createForm({
defaultValues: {
firstName: "",
lastName: ""
}
})
const { register, handleSubmit, reset } = form
const onSubmit = (values) => {
console.log(values)
}
return (
<form onSubmit={handleSubmit(onSubmit)}>
<label>First name</label>
<input {...register("firstName", { required: true })} />
<label>Last name</label>
<input {...register("lastName")} />
<button type="submit">Submit</button>
<button type="reset">Default Reset</button>
<button type="button" onClick={() => reset()}>
Custom Reset
</button>
</form>
)
}