Forms With Formik + TypeScript
Forms With Formik + TypeScript
One of the most painful topics for React developers always was how to
build nice forms and have a nice clean code. Some people might think
that it’s not necessary to use a third-party library, but in some cases,
that’s needed especially when we need more complex forms. First, we
started with Redux Form, a lot of people used it for a long time but then
we started to ask ourselves if it’s a good idea and the best way to
manage our form state using our Redux store. We don’t need to have
our form state in our Redux store, it’s such a not good practice at all.
Then, Formik really came to change it for us and let our forms so easy
to build and our code so readable and well-written that now we don’t
have to worry too much about it. We know that Formik code is written
in TypeScript but a lot of people still don’t know how to use it the right
way.
So, in this article, we’re going to learn about how to use Formik with
TypeScript, since a lot of developers has been started to use it lately,
let’s jump into this hype and see how we can improve more our forms.
https://medium.com/fotontech/forms-with-formik-typescript-d8154cc24f8a 1/8
4/9/2019 Forms with Formik + TypeScript – fotonTech – Medium
Starting
First, let’s start installing some dependencies:
Now, we’re going to start to build our form importing some things that
we’re going to need: we’re going to import the withFormik HOC that
passes our props and form handlers, and also import the FormikProps .
We’re going also import yup to validate our fields.
Now, to start to build our forms, we need first define some interfaces.
Let’s defined an interface called FormValues that’s going to define all
the values that we’re going to have in our form:
1 interface FormValues {
2 email: string;
3 password: string;
4 }
1 interface OtherProps {
2 title?: string;
3 }
Our last interfaces it’s going to be called MyFormProps , and with that
interface, we can define some properties for our initial values, in case
we want to have some initial values.
https://medium.com/fotontech/forms-with-formik-typescript-d8154cc24f8a 2/8
4/9/2019 Forms with Formik + TypeScript – fotonTech – Medium
1 interface MyFormProps {
2 initialEmail?: string;
3 initialPassword?: string;
4 }
Now, we’re going to write our component called InnerForm , pass the
interfaces that we created, and also put some extra code:
https://medium.com/fotontech/forms-with-formik-typescript-d8154cc24f8a 3/8
4/9/2019 Forms with Formik + TypeScript – fotonTech – Medium
https://medium.com/fotontech/forms-with-formik-typescript-d8154cc24f8a 4/8
4/9/2019 Forms with Formik + TypeScript – fotonTech – Medium
First, let’s write our component called App and pass the MyFormProps
1 validationSchema: Yup.object().shape({
2 email: Yup.string()
3 .email("Email not valid")
4 .required("Email is required"),
5 password: Yup.string().required("Password is require
Now, let’s write the handleSubmit function and also pass the
FormValues to validate our props.
Pretty simple, now our whole App component should look like this:
https://medium.com/fotontech/forms-with-formik-typescript-d8154cc24f8a 5/8
4/9/2019 Forms with Formik + TypeScript – fotonTech – Medium
You can find all the code from this article here.
Conclusion
As you can see, Formik is a really helpful lib to let us write better forms
and let our code more readable.
This is a simple example of how to use Formik with TypeScript, but you
can improve it and use it the way you want. The goal here is to show
how to use it the best way and let our code strongly-typed and safer.
https://medium.com/fotontech/forms-with-formik-typescript-d8154cc24f8a 6/8