React-Hook-form 3: Controller, useController(), useformState() and form Context.

The handleSubmit() method accepts two argument functions: the first handles successful form submission, while the second handles failed submission. It returns the form data and the formState.errors object, respectively.

//All validate will be triggered on submit.
const {register, handleSubmit} = useForm()

const valori = (data) => {console.log( "formData:", data )}
const sbagli = (errori) => {console.log( "Returned validate errors:", errori )}

<form onSubmit={handleSubmit(valori, sbagli)}>
  <input {...register("primo", {
    validate: (value)=>{ return value.length > 5 && "Validate error" }
  })}/>
  <input {...register("secondo", {
    maxLength: { value: 4, message: "Built-in error" }
  })}/>
</form>

Any additional errors returned in the callback functions will not be detected and will require a separate error handling.

chevron-righthandleSubmit() on different register() intances and formshashtag

The handleSubmit is a higher-order function that returns a function for form validation and submission.

It accesses inputs registered with the same useForm() instance. It can be triggered by any event, even those outside the form.

The <Controller> input component.

The <Controller/> is a higher-order component that wraps and custom registers an input component. Its properties, such as name, defaultValue, shouldUnregister. and rules, manage the render input registration.

The control prop requires the control object from the useForm() hook, which binds the rendered component to the form and provides the necessary methods and properties to the render component.

The render property renders the <Controller/> component, its props contains the fields, fieldState, and formState objects needed to register, validate, and update the input within the form's state management system.

Their properties work the same as they do in an useForm() input:

  • fields: It handles the registration of the input element and their associated event handlers. Its properties are onChange, onBlur, value, disabled, ref, and name.

  • fieldState: It provides important metadata about the current input field and its validation state. Its properties include invalid, isTouched, isDirty, and error.

  • formState: It shares the formState properties from useForm().

The <Controller/> attributes can set the render component props, like defaultValue being used to specify the initial value of the input field.

The <Controller/> integrates components without needing to access their ref, like with components built without React, such as Material-UI and Ant Design.

The useForm() formState properties can be used within render.

The useController() custom hook.

The useController() custom hook can create multiple and reusable controller inputs. It returns the render props of the <Controller> while using its arguments, it requires the useForm() control object.

The useFormContext() and <FormProvider> component.

The useFormContext() hook accesses the useForm() context provided by the <FormProvider>, without passing the properties trought component props. It enables child components to subscribe to methods and properties from the form context.

We can create <Controller> and useController() inputs within the nested components, using the context control prop.

The useFormState() custom hook.

The useFormState() custom hook subscribes and provides access to the formState properties from outside the <form> component. It optimizes re-renders based on the name input or it destructed formState properties.

chevron-rightThe formState propertieshashtag
  • control: It subscribes to the formState using the control useForm() object.

  • name: optional, it links the formState property to the input, only works for limited properties like dirtyFields and touchedFields, covers the entire formState form if absent.

  • disabled: boolean, it can disable the useFormState() subscription.

  • exact: boolean, limites the name property only to the exact input names.

We can render the name input in the same component as its useFormState().

The returned properties are the same as the formState useForm().

Last updated