In a standard case, we would create a useState or useRef for each field. It would result in a massive amount of code. However, it’s a good approach when you are new to React and still learning. By implementing it yourself, you will learn how React works and how to solve basic problems that surprise beginners. Okay, let’s get back to the main thread.
I have created React with typescript, so let’s create a type that defines our form.
Then is good time to define our validation schema for this model.
const schema: ObjectSchema<FormData> = yup
.object({
firstName: yup.string().required("FirstName cannot be empty"),
lastName: yup.string().required("LastName cannot be empty"),
age: yup.number().positive("Age must be positive number").integer().required("Age cannot be empty").typeError("Age must be a number"),
email: yup.string().required("Email cannot be empty").matches(RegexConsts.email, "Email must contain @ and . characters"),
password: yup.string().required("Password cannot be empty"),
});
Yup provides many validation methods. I won’t waste your time, so here I will only touch on the matches method.
Matches method allows us to define regex pattern to validate this field. The second parameters means failed validation message.
Simplify Your React Forms with React Hook Form and Yup
Here we are! The first post about frontend technology has been written. Until now, only backend and DevOps topics have been covered.
Today I want to show you one of the top libraries that allows us to manage forms easily.
React Hook Form and Yup
What is React Hook Form?
It’s one of the most frequently used library to manage forms with React. As we can read on the project’s site
Performance is one of the primary reasons why this library was created.
https://react-hook-form.com/faqs
What is Yup?
Yup is a library used for creating schemas that help in parsing and validating values at runtime.
I have created simple form using bootstrap.
In a standard case, we would create a useState or useRef for each field. It would result in a massive amount of code. However, it’s a good approach when you are new to React and still learning. By implementing it yourself, you will learn how React works and how to solve basic problems that surprise beginners. Okay, let’s get back to the main thread.
I have created React with typescript, so let’s create a type that defines our form.
Then is good time to define our validation schema for this model.
Yup provides many validation methods. I won’t waste your time, so here I will only touch on the
matches
method.Matches
method allows us to define regex pattern to validate this field. The second parameters means failed validation message.React Hook Form validates forms based on configuration, in this case after submitting the form.
Custom validation for age
When validation is successfully completed, the submit method returns our form object with the filled fields.
The full file looks as presented below
I highly recommend using React Hook Form combined with Yup validation in your daily work with React.
Archives
Understanding K-Nearest-Neighbors (KNN) – Essential Machine Learning Algorithm
2024-10-06A Step-by-Step Guide to Web Scraping for Beginners
2024-07-12Categories
Meta