React Course
React
/
Advanced

Formik

Definition

Formik is one of the most popular legacy libraries for building complex forms in React. It manages form state, validation, and error handling in a highly standardized, declarative way.

Explain Like I'm New

Building forms from scratch means juggling three balls: getting values in/out, validation/error messages, and handling form submission. Formik is an expert juggler you hire to handle all three balls for you, so you can just focus on how the form looks.

Real World Example

Many large enterprise applications built between 2018 and 2021 use Formik heavily alongside the Yup validation library. It provides components like `<Formik>`, `<Form>`, `<Field>`, and `<ErrorMessage>` to rapidly build UI.

Common Use Cases

  • Maintaining older React codebases
  • Rapidly scaffolding forms using its built-in Context-based components (`<Field>`)

Interactive Example

/*
// Conceptual Example - Requires npm install formik
import { Formik, Form, Field, ErrorMessage } from "formik";

export default function FormikExample() {
  return (
    <Formik
      initialValues={{ email: "", password: "" }}
      validate={values => {
        const errors = {};
        if (!values.email) errors.email = "Required";
        return errors;
      }}
      onSubmit={(values, { setSubmitting }) => {
        console.log(values);
        setSubmitting(false);
      }}
    >
      {/* Formik uses Render Props or Context to provide state to children */}
      {({ isSubmitting }) => (
        <Form>
          {/* <Field> automatically wires up value and onChange! */}
          <Field type="email" name="email" />
          <ErrorMessage name="email" component="div" />
          
          <Field type="password" name="password" />
          <button type="submit" disabled={isSubmitting}>
            Submit
          </button>
        </Form>
      )}
    </Formik>
  );
}
*/
console.log("Formik is powerful, but React Hook Form has largely superseded it in modern apps due to performance.");

Interview Questions

basic

  • What are the three main problems Formik solves?
  • What validation library is most commonly paired with Formik?

intermediate

  • What is the difference between Formik and React Hook Form?
  • How does the `<Field>` component work?

advanced

  • What performance issues can arise when using Formik on massive forms?
  • What is `useFormikContext` used for?

trick

  • Does Formik use uncontrolled or controlled components?

Flash Cards

Question

What is the difference between Formik and React Hook Form?

Click to reveal answer
Answer

Formik is strictly a Controlled component architecture. Every keystroke updates Formik's state and causes the form to re-render. React Hook Form is Uncontrolled, meaning keystrokes don't cause re-renders, making it significantly faster for large forms.

Question

What validation library is paired with it?

Click to reveal answer
Answer

Yup. Formik has a built-in prop literally called `validationSchema` that is specifically designed to accept a Yup schema object.

Question

Does Formik use controlled or uncontrolled components?

Click to reveal answer
Answer

Controlled. Formik stores the entire form state in a central React state object and forces it down to the inputs via `value` and `onChange`.