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?