</> useFormState: (UseFormStateProps) => FormState
This custom hook allows you to subscribe to each form state, and isolate re-renders at the custom hook level. It has its own scope in terms of form state subscription, so it does not affect other useFormState or useForm hooks. Using this hook can reduce the re-render impact on large and complex form applications.
Props
| Name | Type | Description |
|---|---|---|
control | Object | control object provided by useForm. It's optional if you are using FormProvider. |
name | string | string[] | Since v7.4.0 Provide a single input name, an array of them, or subscribe to all inputs' formState updates. |
disabled | boolean = false | Since v7.13.0 Option to disable the subscription. |
exact | boolean = false | Since v7.20.0 This prop will enable an exact match for input name subscriptions. |
Return
| Name | Type | Description |
|---|---|---|
isDirty | boolean | Set to true after the user modifies any of the inputs.
|
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 defaultValues.
|
touchedFields | object | An object containing all the inputs the user has interacted with. |
defaultValues | object | Since v7.37.0 The value that was set in 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 | Indicates that the form was successfully submitted without any runtime error. |
isSubmitting | boolean | true if the form is currently being submitted. false otherwise. |
isLoading | boolean | Since v7.41.0 true if the form is currently loading async default values.
|
submitCount | number | The 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 | object | Since v7.51.0 Captures fields that are undergoing asynchronous validation. |
errors | object | An object with field errors. There is also an ErrorMessage component to retrieve error messages easily. |
disabled | boolean | Since v7.48.0 Set to true if the form is disabled via the disabled prop in useForm. |
isReady | boolean | Since v7.56.0 Set to true when formState subscription setup is ready. |
RULES
Returned formState is wrapped with a Proxy to improve render performance and skip extra computation if a specific state is not subscribed to; make sure you destructure or read it before rendering to enable the subscription.
const { isDirty } = useFormState() // ✅const formState = useFormState() // ❌ should destructure the formState
Examples
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>)}
Thank you for your support
If you find React Hook Form to be useful in your project, please consider starring and supporting it.