useFormState: ({ control: Control }) => FormState
This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
Props
The following table contains information about the arguments for useFormState
.
Name | Type | Description |
---|---|---|
control | object | control object provided by useForm . It's optional if you are using FormProvider . |
name | string | string[] | Provide a single input name, an array of them, or subscribe to all inputs' formState update. |
disabled | boolean = false | Option to disable the subscription. |
exact | boolean = false | This prop will enable an exact match for input name subscriptions. |
Return
Name | Type | Description |
---|---|---|
isDirty | boolean | Set to
|
dirtyFields | object | An object with the user-modified fields. Make sure to provide all inputs' defaultValues via useForm, so the library can compare against the
|
touchedFields | object | An object containing all the inputs the user has interacted with. |
defaultValues | object | The value which has been set at useForm's defaultValues or updated defaultValues via reset API. |
isSubmitted | boolean | Set to true after the form is submitted. Will remain true until the reset method is invoked. |
isSubmitSuccessful | boolean | Indicate the form was successfully submitted without any runtime error. |
isSubmitting | boolean | true if the form is currently being submitted. false otherwise. |
isLoading | boolean |
Important: this prop is only applicable to async
|
submitCount | number | Number of times the form was submitted. |
isValid | boolean | Set to true if the form doesn't have any errors.
|
isValidating | boolean | Set to true during validation. |
validatingFields | boolean | Capture fields which are getting async validation. |
errors | object | An object with field errors. There is also an ErrorMessage component to retrieve error message easily. |
Rules
Returned formState
is wrapped with Proxy to improve render performance and skip extra computation if specific state is not subscribed, so make sure you deconstruct or read it before render in order to enable the subscription.
const { isDirty } = useFormState(); // ✅const formState = useFormState(); // ❌ should deconstruct the formState
Examples
import * as React from "react";import { useForm, useFormState } from "react-hook-form";function Child({ control }) {const { dirtyFields } = useFormState({control});return dirtyFields.firstName ? <p>Field is dirty.</p> : null;};export default function App() {const { register, handleSubmit, control } = useForm({defaultValues: {firstName: "firstName"}});const onSubmit = (data) => console.log(data);return (<form onSubmit={handleSubmit(onSubmit)}><input {...register("firstName")} placeholder="First Name" /><Child control={control} /><input type="submit" /></form>);}