Skip to main content

Command Palette

Search for a command to run...

Getting Started with Formik: A Comprehensive Guide

Updated
3 min read
Getting Started with Formik: A Comprehensive Guide
A

Full-stack software engineer with 2.5 years of experience building reliable, scalable web apps using JavaScript, TypeScript, React, Next.js, Node.js, and MongoDB. I enjoy creating clean, reusable UI components, integrating APIs, and improving performance through web vitals, code-splitting, and smart bundling. I regularly use Claude, GPT, Cursor AI, and custom automation workflows to speed up development and solve problems faster. I’ve also worked with AI agents, LLMs to simplify workflows and make features more intuitive for users. Above all, I care about writing maintainable code, collaborating well with teams, and designing systems that stay fast, stable, and easy to scale.

Formik is a popular and widely-used library for managing forms in React applications. It simplifies the process of handling form state, validation, and submission, making it easier for developers to build efficient and maintainable forms. In this blog, we'll dive into the world of Formik, exploring its features and showcasing its capabilities through code snippets.

Introduction to Formik

React provides excellent support for handling form inputs and maintaining state. However, as forms grow in complexity, managing form state, validation, and submission can become cumbersome. Formik comes to the rescue, providing a simple yet powerful abstraction over forms, making it easier to manage complex forms with ease.

Formik's primary features include:

  • Managing form state

  • Handling form submissions

  • Validating form input

  • Displaying error messages

Let's explore these features step-by-step with examples.

Installing and Setting Up Formik

To get started with Formik, you need to install the package using npm or yarn:

npm install formik

or

yarn add formik

Now, you can import the Formik component and use it in your React application. Let's create a simple form with Formik.

Creating a Simple Form with Formik

Here's a basic example of a form with Formik:

import React from 'react';
import { Formik, Form, Field } from 'formik';

const SimpleForm = () => {
  return (
    <Formik
      initialValues={{ name: '' }}
      onSubmit={(values) => {
        console.log('Form values: ', values);
      }}
    >
      {() => (
        <Form>
          <label htmlFor="name">Name:</label>
          <Field id="name" name="name" placeholder="Enter your name" />
          <button type="submit">Submit</button>
        </Form>
      )}
    </Formik>
  );
};

export default SimpleForm;

In this example, we have:

  • Imported Formik, Form, and Field components from the Formik library.

  • Defined the initial form values using the initialValues prop.

  • Implemented the onSubmit function to handle form submission.

Validating Form Input

Formik makes it easy to validate form inputs using the validate prop or the validationSchema prop with Yup. Let's add validation to our example using Yup.

First, install Yup:

npm install yup

or

yarn add yup

Now, let's add validation to our form:

import React from 'react';
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';

const validationSchema = Yup.object({
  name: Yup.string().required('Name is required!'),
});

const SimpleForm = () => {
  return (
    <Formik
      initialValues={{ name: '' }}
      validationSchema={validationSchema}
      onSubmit={(values) => {
        console.log('Form values: ', values);
      }}
    >
      {() => (
        <Form>
          <label htmlFor="name">Name:</label>
          <Field id="name" name="name" placeholder="Enter your name" />
          <ErrorMessage name="name" />
          <button type="submit">Submit</button>
        </Form>
      )}
    </Formik>
  );
};

export default SimpleForm;

In this example, we have:

  • Imported ErrorMessage from the Formik library.

  • Created a validation schema using Yup.

  • Passed the validation schema to the Formik component using the validationSchema prop.

  • Displayed error messages using the <ErrorMessage> component.

Conclusion

Formik is a powerful library that simplifies form handling in React applications. It provides an easy-to-use abstraction for managing form state, validation, and submission. With Formik, you can build efficient and maintainable forms without the hassle of managing complex state and validation logic.

In this blog, we have explored the basics of Formik, including installation, creating a simple form, and adding validation. With these building blocks, you can now create more complex forms and harness the power of Formik in your React applications. Happy coding!