Skip to content
tezvyn:

Formik: Taming React Form State

Source: formik.orgMediumHow cards are made

Formik: Taming React Form State

Formik is a state machine for your React forms, managing values, errors, and visited fields without providing UI. Use it to avoid boilerplate for validation and submission. The footgun is fighting its design by trying to integrate it with global state stores.

Why it exists

Raw HTML forms in React are surprisingly verbose. You need to manage the state of every input, handle onChange events, track validation errors, and manage the submission state. This leads to a lot of repetitive boilerplate code that is prone to bugs and hard to maintain.

The mental model

Think of Formik as a dedicated state manager just for your form. It's a controller that sits alongside your form components, handling the data and logic (what's the value, is it valid, has it been touched?) but not the presentation (the look of the input field). It keeps form state local and ephemeral, intentionally avoiding global state managers like Redux.

How it works

Formik provides a component or a hook (useFormik) that gives you everything needed to build a form. This includes the form's state (values, errors, touched), and helper functions to handle changes (handleChange), blurs (handleBlur), and submission (handleSubmit). You connect these helpers to your standard HTML input elements. Formik orchestrates the flow, running validation (often with a library like Yup) and updating the state as the user interacts with the form.

When to use it

Use Formik when your form has more than a couple of fields, requires client-side validation, needs to display error messages per-field, or involves multi-step 'wizard' flows. It shines when you want to reduce boilerplate without adding a heavy, opinionated UI library. It's also great for forms that need to handle array fields, like adding multiple phone numbers.

When not to use it

For very simple forms with one or two inputs and no validation, Formik can be overkill; plain React state (useState) is simpler. If you need a library that provides styled, ready-to-use UI components, Formik is not it—it's unopinionated about your UI. It's also less ideal if your team is already standardized on a different solution like React Hook Form.

One canonical example

A user registration form. It needs to track username, email, and password. It must validate that the email is a valid format and the password meets strength requirements. Formik would manage the state for all inputs, hold the error messages for each, and prevent submission until all fields are valid, all without you manually wiring up dozens of state variables and event handlers.

Interview question

Which statement best characterizes Formik's role in a React application?

  • a.It primarily focuses on server-side validation, reducing the need for client-side checks.
  • b.It functions as a dedicated state machine for form data and logic, independent of UI presentation.Correct
  • c.It offers a comprehensive UI component library for building visually consistent forms.
  • d.It integrates form state directly into a global application state management solution like Redux.
Why?

Formik is described as a "dedicated state manager just for your form" that handles "data and logic" but "not the presentation." It explicitly avoids providing UI components and is designed to keep form state local, not integrated with global state managers.

Just read this? Test yourself on what you have been reading.

Read the original → formik.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles