Top Related Projects
A Higher Order Component using react-redux to keep form state in a Redux store
📋 React Hooks for form state management and validation (Web + React Native)
🏁 High performance subscription-based form state management for React
Performance-focused API for React forms 🚀
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
Build forms in React, without the tears 😭
Quick Overview
Formik is a popular React library for building and managing forms. It simplifies the process of handling form state, validation, and submission, reducing boilerplate code and making form development more efficient and less error-prone.
Pros
- Simplifies form state management and reduces boilerplate code
- Provides built-in validation and error handling
- Integrates well with other React libraries and ecosystems
- Offers excellent TypeScript support
Cons
- Learning curve for beginners, especially those new to React
- Can be overkill for very simple forms
- Performance may be impacted for extremely large and complex forms
- Some users report difficulties with advanced use cases or custom implementations
Code Examples
- Basic form setup:
import { Formik, Form, Field } from 'formik';
const BasicForm = () => (
<Formik
initialValues={{ email: '', password: '' }}
onSubmit={(values) => console.log(values)}
>
<Form>
<Field name="email" type="email" />
<Field name="password" type="password" />
<button type="submit">Submit</button>
</Form>
</Formik>
);
- Form with validation:
import { Formik, Form, Field, ErrorMessage } from 'formik';
import * as Yup from 'yup';
const validationSchema = Yup.object({
email: Yup.string().email('Invalid email').required('Required'),
password: Yup.string().min(6, 'Too short').required('Required'),
});
const ValidatedForm = () => (
<Formik
initialValues={{ email: '', password: '' }}
validationSchema={validationSchema}
onSubmit={(values) => console.log(values)}
>
<Form>
<Field name="email" type="email" />
<ErrorMessage name="email" component="div" />
<Field name="password" type="password" />
<ErrorMessage name="password" component="div" />
<button type="submit">Submit</button>
</Form>
</Formik>
);
- Custom field component:
import { useField } from 'formik';
const CustomInput = ({ label, ...props }) => {
const [field, meta] = useField(props);
return (
<>
<label htmlFor={props.id || props.name}>{label}</label>
<input {...field} {...props} />
{meta.touched && meta.error ? (
<div className="error">{meta.error}</div>
) : null}
</>
);
};
Getting Started
To start using Formik in your React project:
-
Install Formik:
npm install formik
-
Import and use Formik in your component:
import { Formik, Form, Field } from 'formik'; const MyForm = () => ( <Formik initialValues={{ name: '' }} onSubmit={(values) => console.log(values)} > <Form> <Field name="name" type="text" /> <button type="submit">Submit</button> </Form> </Formik> );
-
For validation, consider installing Yup:
npm install yup
Competitor Comparisons
A Higher Order Component using react-redux to keep form state in a Redux store
Pros of Redux Form
- Integrates seamlessly with Redux, making it ideal for applications already using Redux
- Offers powerful form state management and synchronization across components
- Provides robust validation and normalization features out-of-the-box
Cons of Redux Form
- Requires Redux as a dependency, which may be unnecessary for smaller projects
- Has a steeper learning curve due to its tight coupling with Redux concepts
- Can lead to increased boilerplate code and complexity in form handling
Code Comparison
Redux Form:
import { reduxForm, Field } from 'redux-form';
const MyForm = ({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field name="username" component="input" type="text" />
<button type="submit">Submit</button>
</form>
);
export default reduxForm({ form: 'myForm' })(MyForm);
Formik:
import { Formik, Field, Form } from 'formik';
const MyForm = () => (
<Formik initialValues={{ username: '' }} onSubmit={handleSubmit}>
<Form>
<Field name="username" type="text" />
<button type="submit">Submit</button>
</Form>
</Formik>
);
export default MyForm;
Both libraries offer powerful form management capabilities, but Formik provides a more lightweight and flexible solution, especially for projects not already using Redux. Redux Form excels in complex Redux-based applications, while Formik is more adaptable and easier to integrate into various React projects.
📋 React Hooks for form state management and validation (Web + React Native)
Pros of react-hook-form
- Smaller bundle size and better performance
- Simpler API with less boilerplate code
- Built-in validation with easy integration of custom validation rules
Cons of react-hook-form
- Less opinionated, which may require more setup for complex forms
- Fewer built-in features compared to Formik (e.g., no built-in form-level validation)
Code Comparison
react-hook-form:
import { useForm } from "react-hook-form";
function App() {
const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("firstName")} />
<input type="submit" />
</form>
);
}
Formik:
import { Formik, Form, Field } from 'formik';
function App() {
return (
<Formik
initialValues={{ firstName: '' }}
onSubmit={(values) => console.log(values)}
>
<Form>
<Field name="firstName" />
<button type="submit">Submit</button>
</Form>
</Formik>
);
}
Both libraries offer efficient ways to handle forms in React, but react-hook-form provides a more lightweight and flexible approach, while Formik offers a more structured and opinionated solution with additional built-in features.
🏁 High performance subscription-based form state management for React
Pros of React Final Form
- Smaller bundle size and better performance
- More flexible and customizable
- Supports both synchronous and asynchronous validation out of the box
Cons of React Final Form
- Less opinionated, which may require more setup and configuration
- Smaller community and ecosystem compared to Formik
- Steeper learning curve for beginners
Code Comparison
React Final Form:
import { Form, Field } from 'react-final-form'
<Form
onSubmit={onSubmit}
render={({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field name="firstName" component="input" placeholder="First Name" />
</form>
)}
/>
Formik:
import { Formik, Field, Form } from 'formik'
<Formik
initialValues={{ firstName: '' }}
onSubmit={onSubmit}
>
<Form>
<Field name="firstName" placeholder="First Name" />
</Form>
</Formik>
Both libraries offer similar functionality for handling form state and validation in React applications. React Final Form provides more flexibility and better performance, while Formik offers a more opinionated and beginner-friendly approach. The choice between the two depends on project requirements, team expertise, and personal preferences.
Performance-focused API for React forms 🚀
Pros of Unform
- More flexible and customizable, allowing for easier integration with custom form elements
- Lighter weight and potentially better performance due to its minimalist approach
- Built with TypeScript, offering better type safety and developer experience
Cons of Unform
- Smaller community and ecosystem compared to Formik
- Less comprehensive documentation and fewer examples available
- May require more manual setup and configuration for complex form scenarios
Code Comparison
Formik:
import { Formik, Form, Field } from 'formik';
<Formik initialValues={{ name: '' }} onSubmit={handleSubmit}>
<Form>
<Field name="name" />
<button type="submit">Submit</button>
</Form>
</Formik>
Unform:
import { Form } from '@unform/web';
import Input from './components/Input';
<Form onSubmit={handleSubmit}>
<Input name="name" />
<button type="submit">Submit</button>
</Form>
Both libraries aim to simplify form handling in React applications, but they differ in their approach and feature set. Formik offers a more opinionated and comprehensive solution, while Unform provides a more flexible and lightweight alternative. The choice between the two depends on project requirements, team preferences, and the level of customization needed for form handling.
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
Pros of TanStack Form
- More flexible and framework-agnostic, supporting React, Vue, Solid, and vanilla JS
- Lighter weight and potentially better performance due to its minimalist approach
- Offers more granular control over form state and validation
Cons of TanStack Form
- Steeper learning curve due to its lower-level API
- Less opinionated, which may require more setup and configuration
- Smaller community and ecosystem compared to Formik
Code Comparison
Formik:
import { Formik, Form, Field } from 'formik';
<Formik initialValues={{ email: '' }} onSubmit={handleSubmit}>
<Form>
<Field name="email" type="email" />
<button type="submit">Submit</button>
</Form>
</Formik>
TanStack Form:
import { useForm } from '@tanstack/react-form'
const form = useForm({
defaultValues: { email: '' },
onSubmit: handleSubmit,
})
<form.Provider>
<form onSubmit={form.handleSubmit}>
<input {...form.register('email')} />
<button type="submit">Submit</button>
</form>
</form.Provider>
Both libraries aim to simplify form management in React applications, but they take different approaches. Formik provides a more opinionated and higher-level API, while TanStack Form offers more flexibility and control at the cost of additional setup.
Build forms in React, without the tears 😭
Pros of Formik
- Well-established and widely adopted form management library for React
- Extensive documentation and community support
- Integrates seamlessly with popular validation libraries like Yup
Cons of Formik
- Larger bundle size compared to newer alternatives
- Can be overkill for simple form implementations
- Learning curve for complex use cases
Code Comparison
Formik:
import { Formik, Form, Field } from 'formik';
<Formik initialValues={{ name: '' }} onSubmit={handleSubmit}>
<Form>
<Field name="name" />
<button type="submit">Submit</button>
</Form>
</Formik>
Since both repositories refer to the same project (Formik), there isn't a different code example to compare. The code snippet above demonstrates the basic usage of Formik in a React application.
Additional Notes
Formik is a popular form management library for React applications. It provides a set of tools and components to handle form state, validation, and submission. While it offers robust features and extensive documentation, some developers may find it overly complex for simple use cases. Alternatives like React Hook Form have emerged, offering lighter-weight solutions for form management in React applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Build forms in React, without the tears.
Visit https://formik.org to get started with Formik.
Organizations and projects using Formik
List of organizations and projects using Formik
Authors
- Jared Palmer @jaredpalmer
- Ian White @eonwhite
Contributing
This monorepo uses yarn
, so to start you'll need the package manager installed.
To run E2E tests you'll also need Playwright set up, which can be done locally via npx playwright install
. Afterward, run yarn start:app
and in a separate tab run yarn e2e:ui
to boot up the test runner.
When you're done with your changes, we use changesets to manage release notes. Run yarn changeset
to autogenerate notes to be appended to your pull request.
Thank you!
Contributors
Formik is made with <3 thanks to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Related
- TSDX - Zero-config CLI for TypeScript used by this repo. (Formik's Rollup configuration as a CLI)
Top Related Projects
A Higher Order Component using react-redux to keep form state in a Redux store
📋 React Hooks for form state management and validation (Web + React Native)
🏁 High performance subscription-based form state management for React
Performance-focused API for React forms 🚀
🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.
Build forms in React, without the tears 😭
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot