Formik And React is a serie of tutorials where we'll walk through how to build and validate forms in React with Formik and Yup. In addition to the basics, I'll give you tips on how to structure and customize your field components to make building forms even easier.
In this first tutorial we will talk about the basics of Formik starting from installing and importing the library to structuring a Formik form, but before diving deeper into subject let's have a brief overview of the library.
What is Formik ?
Forms are used to collect user data on web applications, still sometimes we need to collect large amounts of data which may result in a large form with several fields and validation control. And this can be a pain for both users and developers. That's how Formik was born, and from the very beginning they made it clear :
Build forms in React, without the tears 😭 - Jared Palmer, Creator of Formik.
Basically Formik makes forms in React easy by :
- Managing your form's state
- Providing validation on all of your fields.
- Handling form submission
You can simply add Formik as a dependency to your project: using
yarn as follows:
yarn add formik
In this tutorial we're going to start with the detailed way of using Formik, because after all it's important to see how Formik builds so we can have a full look of what's possible and how it works.
Let's create a simple form to understand the basics, we will start with a user info form for a web site. For now our form will have just one username field. Using Formik, it's easy :
useFormik() hook we will pass to our form :
- the submission function
useFormik() will return a collection of helper methods, for now we will use: :
- Submission handler :
- A change handler to pass to each
- the current form values :
In the exemple above, we can't tell the benefits of using
useFormik() as we only have one input. So let's add two more inputs: one for the user's email and the other for the full name, which we'll store as
fullName in the form.
Let's take a look at our new code, you'll notice that :
- We used the same change handler
handleChangefor all the inputs
- We pass the
nameattribute that matches the property we defined in
initialValues, so that Formik can handle their state for you.
- We can get access to the field's value using
For now, our form doesn't tell the users which fields are required. To do so we can use the browser's built-in HTML input validation by adding
required prop to each of our inputs, we can also add a
pattern prop for regex validation for each of these inputs. This is great for beginners but HTML validation has its limitations. First, it only works in the browser! So we can't use it for example with a framework like React Native. Second, it's very hard to show custom error messages and finally it's very janky.
As mentioned earlier, Formik keeps track of not only your form's
values, but also its error messages and validation.
Let's add a custom validation function
validate to the
useFormik() hook, this function will produce an
error if an error exists ( the error object will be matching shape with our
There is a lot more to learn about Formik. This tutorial is just the start and we barely began. In this tutorial we saw how to create your first form using Formik and how to use custom validation functions for Validation. In the next tutorial we will dig a little bit deeper and we will use Yup wich is a library that allows you to perform validation with Formik in a more elegant way.