Using React Hook Form
In this section, we will learn about React Hook Form and use it to improve the validation user experience in our contact form.
Understanding React Hook Form
As the name suggests, React Hook Form is a React library for building forms. It is very flexible and can be used for simple forms such as our contact form, as well as large forms with complex validation and submission logic. It is also very performant and optimised not to cause unnecessary re-renders. It is also very popular with tens of thousands of GitHub stars and maturing nicely having been first released in 2019.
The key part of React Hooks Form is a useForm
hook, which returns useful functions and the state. The following code snippet shows the useForm
hook being called:
const { register, handleSubmit, formState: { errors, isSubmitting, isSubmitSuccessful } } = useForm<FormData>();
useForm
has a generic type parameter for the type of the field...