useController:
(props?: UseControllerProps) => { field: object, fieldState: object, formState: object }
This custom hook powers Controller
. Additionally, it shares the same props and methods as Controller
. It's useful for creating reusable Controlled input.
Props
The following table contains information about the arguments for useController
.
Name | Type | Required | Description |
---|---|---|---|
name | FieldPath | ✓ | Unique name of your input. |
control | Control | control object provided by invoking useForm . Optional when using FormProvider . | |
defaultValue | unknown | Important: Can not apply
| |
rules | Object | Validation rules in the same format for required, min, max, minLength, maxLength, pattern, validate | |
shouldUnregister | boolean = false | Input will be unregistered after unmount and defaultValues will be removed as well. |
Return
The following table contains information about properties which useController
produces.
Object Name | Name | Type | Description |
---|---|---|---|
field | onChange | (value: any) => void | A function which sends the input's value to the library. |
onBlur | () => void | A function which sends the input's onBlur event to the library. It should be assigned to the input's | |
value | unknown | The current value of the controlled component. | |
name | |||
Input's name being registered. | |||
ref | |||
A ref used to connect hook form to the input. Assign | |||
fieldState | invalid | boolean | Invalid state for current input. |
isTouched | boolean | Touched state for current controlled input. | |
isDirty | boolean | Dirty state for current controlled input. | |
error | object | error for this specific input. | |
formState | 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 Dirty fields will not represent as | |
touchedFields | object | An object containing all the inputs the user has interacted with. | |
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 | |
isSubmitting | boolean | true if the form is currently being submitted. false otherwise. | |
submitCount | number | Number of times the form was submitted. | |
isValid | boolean | Set to true if the form doesn't have any errors.Note: | |
isValidating | boolean | Set to true during validation. | |
errors | object | An object with field errors. There is also an ErrorMessage component to retrieve error message easily. |
Examples
Tips
Do not
register
input again. This custom hook is designed to take care of the registration process.It's ideal to use a single
useController
per component. If you need to use more than one, make sure you rename the prop. May want to consider usingController
instead.
Thank you for your support
If you find React Hook Form to be useful in your project, please consider to star and support it.