Every React form needs the same things: values for each field, errors for each field, a touched flag so errors only show after the user interacts, and a submit handler. Writing that by hand for every form is tedious. Formik is a library that manages all of it for you.
Here is a signup form with Formik:
import { Formik, Form, Field, ErrorMessage } from 'formik';
function SignupForm() {
return (
<Formik
initialValues={{ email: '', password: '' }}
validate={(values) => {
const errors = {};
if (!values.email) errors.email = 'Email is required';
if (values.password.length < 8) {
errors.password = 'Password must be at least 8 characters';
}
return errors;
}}
onSubmit={(values) => console.log(values)}
>
<Form>
<Field name="email" type="email" placeholder="Email" />
<ErrorMessage name="email" component="p" />
<Field name="password" type="password" placeholder="Password" />
<ErrorMessage name="password" component="p" />
<button type="submit">Sign up</button>
</Form>
</Formik>
);
}
Formik tracks values, errors, and touched state for every field automatically. Validation can be a simple function like above, or a Yup schema for more complex rules. Error messages appear only after a field has been touched, which is the behaviour users expect.
Formik also handles the messy parts: submitting states so you can disable the button while saving, resetting the form after submit, and nested or array fields for things like a list of phone numbers. It works with any UI library, and there is a useFormik hook if you prefer hooks over components.
Formik is for React developers building forms with more than a couple of fields — signup, checkout, settings, or admin panels. It removes the repetitive state management and lets you focus on the fields and the validation rules that actually matter.
Top comments (0)