What is formik?
Formik is a popular open-source library for building forms in React and React Native. It helps with handling form state, validation, and submission. Formik provides a simple and efficient way to create controlled form components with less boilerplate code.
What are formik's main functionalities?
Form State Management
Formik simplifies form state management by providing 'initialValues' to set up the form state and 'onSubmit' to handle form submission.
{"<Formik initialValues={{ name: '' }} onSubmit={(values) => { console.log(values); }}><Form><Field name='name' type='text' /><button type='submit'>Submit</button></Form></Formik>"}
Validation and Error Handling
Formik provides a 'validate' function to define custom validation logic and uses 'ErrorMessage' to display validation errors.
{"<Formik initialValues={{ email: '' }} validate={values => { const errors = {}; if (!values.email) { errors.email = 'Required'; } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(values.email)) { errors.email = 'Invalid email address'; } return errors; }} onSubmit={(values, { setSubmitting }) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); }}><Form><Field name='email' type='email' /><ErrorMessage name='email' component='div' /><button type='submit'>Submit</button></Form></Formik>"}
Integration with Yup for Schema Validation
Formik can be integrated with Yup, a schema builder for value parsing and validation, to simplify form validation using a schema.
{"<Formik initialValues={{ email: '' }} validationSchema={Yup.object({ email: Yup.string().email('Invalid email address').required('Required'), })} onSubmit={(values, { setSubmitting }) => { setTimeout(() => { alert(JSON.stringify(values, null, 2)); setSubmitting(false); }, 400); }}><Form><Field name='email' type='email' /><ErrorMessage name='email' /><button type='submit'>Submit</button></Form></Formik>"}
Custom Input Components
Formik allows the use of custom input components with the 'as' prop in the 'Field' component, enabling the creation of reusable form elements.
{"<Formik initialValues={{ name: '' }} onSubmit={(values) => { console.log(values); }}><Form><Field name='name' as={MyCustomInput} /><button type='submit'>Submit</button></Form></Formik>"}
Other packages similar to formik
react-hook-form
React Hook Form is a library for managing forms in React. It uses hooks for form state management and is optimized for performance by minimizing the number of re-renders. Compared to Formik, React Hook Form is often considered faster due to its leaner API and focus on reducing re-renders.
redux-form
Redux Form leverages Redux for form state management. It integrates tightly with Redux and allows form state to be stored in the Redux store. Compared to Formik, Redux Form is more suitable for applications that already use Redux and require complex form state management across multiple components.
final-form
Final Form is a framework-agnostic form library that can be used with React via 'react-final-form'. It focuses on performance and flexibility, offering subscription-based form state management. Compared to Formik, Final Form provides more fine-grained control over form state updates and subscriptions.
Formik Next
Experimental features. There will be tears. If you are not Jared or Ian, use at your own risk.
Let's face it, forms are really verbose in React. To make matters worse, most form helpers do wayyyy too much magic and often have a significant performance cost associated with them. Formik is a minimal Higher Order Component that helps you with the 3 most annoying parts:
- Transforming props to form state
- Validation and error messages
- Handling form submission
By colocating all of the above in one place, Formik will keep things organized--making testing, refactoring, and reasoning about your forms a breeze.
Installation
Add Formik (and optionally Yup to your project). Formik supports/recommends Yup (which is like Joi, but for the browser) for object schema validation.
npm i formik@next yup --save
Note: Yup is 100% optional. You are free to [write your own validators][validate
].
You can also try before you buy with this demo of Formik on CodeSandbox.io
Table of Contents
<Formik />
Formik is now a component that uses render props!
API change from master: mapValuesToProps
doesn't exist. Just pass an object to getInitialValues
instead.
Aside from that, <Formik />
= Formik()
except with a lot less ceremony...
import { Formik } from 'formik/next'
interface Values {
name: string
}
const BasicExample: React.SFC<...> = () =>
<div>
<h1>My Form</h1>
<Formik
getInitialValues={{ name: 'jared' }}
handleSubmit={(values: Values) => {
setTimeout(() => alert(JSON.stringify(values, null, 2)), 1000);
}}
render={(props: FormComponentProps<Values>) =>
<form onSubmit={props.handleSubmit}>
<input
type="text"
onChange={props.handleChange}
onBlur={props.handleBlur}
value={props.values.name}
name="name"
/>
{props.errors.name &&
<div id="feedback">
{props.errors.name}
</div>}
<button type="submit">Submit</button>
</form>}
/>
</div>;
You can avoid render callbacks all together too... #perf
import { Formik } from 'formik/next'
interface Values {
name: string
}
class BasicClassExample extends React.Component<any, any> {
handleSubmit = (values: Values) => {
setTimeout(() => alert(JSON.stringify(values, null, 2)), 1000);
}
render() {
return (
<div>
<h1>My Form</h1>
<Formik
getInitialValues={{ name: 'jared' }}
handleSubmit={handleSubmit}
component={ContactForm}
/>
</div>
);
}
}
const ContactForm: React.SFC<FormComponentProps<Values>> = ({
handleSubmit,
handleChange,
handleBlur,
values,
errors
}) => {
return
<form onSubmit={props.handleSubmit}>
<input
type="text"
onChange={props.handleChange}
onBlur={props.handleBlur}
value={props.values.name}
name="name"
/>
{props.errors.name &&
<div>
{props.errors.name}
</div>}
<button type="submit">Submit</button>
</form>
}
Formik Render Methods
There are now three ways to render things with Formik
<Formik component>
<Formik render>
<Formik children>
Formik props
All three render methods will be passed the same three route props:
- dirty
- errors
- handleBlur
- handleChange
- handleReset
- handleSubmit
- isSubmitting
- isValid
- resetForm
- setErrors
- setFieldError
- setFieldTouched
- setFieldValue
- setStatus
- setSubmitting
- setTouched
- setValues
- status
- touched
- values
component
<Formik component={ContactForm} />
const ContactForm = ({ handleSubmit, handleChange, handleBlur, values, errors }) => {
return
<form onSubmit={props.handleSubmit}>
<input
type="text"
onChange={props.handleChange}
onBlur={props.handleBlur}
value={props.values.name}
name="name"
/>
{props.errors.name &&
<div>
{props.errors.name}
</div>}
<button type="submit">Submit</button>
</form>
}
Warning: <Formik component>
takes precendence over <Formik render>
so don’t use both in the same <Formik>
.
render: (props: FormComponentProps<Values>) => ReactNode
<Formik render={props => <ContactForm {...props} />}/>
<Formik
render={({ handleSubmit, handleChange, handleBlur, values, errors }) => (
<form onSubmit={props.handleSubmit}>
<input
type="text"
onChange={props.handleChange}
onBlur={props.handleBlur}
value={props.values.name}
name="name"
/>
{props.errors.name &&
<div>
{props.errors.name}
</div>}
<button type="submit">Submit</button>
</form>
)}
/>
children: func
<Formik children={props => <ContactForm {...props} />}/>
<Formik>
{({ handleSubmit, handleChange, handleBlur, values, errors }) => (
<form onSubmit={props.handleSubmit}>
<input
type="text"
onChange={props.handleChange}
onBlur={props.handleBlur}
value={props.values.name}
name="name"
/>
{props.errors.name &&
<div>
{props.errors.name}
</div>}
<button type="submit">Submit</button>
</form>
)}
</Formik>
<Field />
(Much Experimental. Very magic )
<Field />
will automagically hook up inputs to Formik. It uses the name
attribute to match up with Formik state. <Field/>
will default to and <input/>
element. To change the underlying element of <Field/>
, specify a component
prop. It can either be a string like select
or another React component.
import * as React from 'react';
import { Formik, Field, FormComponentProps } from 'formik/next';
interface Values {
email: string;
color: string;
firstName: string;
}
const Example: React.SFC<...> = () => (
<div>
<h1>My Form</h1>
<Formik
getInitialValues={{ email: '', color: 'red', firstName: '' }}
handleSubmit={(values: Values) => {
setTimeout(() => alert(JSON.stringify(values, null, 2)), 1000);
}}
render={(props: FormComponentProps<Values>) =>
<form onSubmit={props.handleSubmit}>
<Field type="email" name="email" placeholder="Email" />
<Field component="select" name="color" >
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</Field>
<Field component={CustomInputComponent} name="firstName" />
<button type="submit">Submit</button>
</form>}
/>
</div>
);
const CustomInputComponent: React.SFC<FormComponentProps<Values> & CustomInputProps> => ({
name,
placeholder,
values,
errors,
handleBlur,
handleChange,
...props
}) => (
<div>
<input
className="custom"
type="text"
value={values[name]}
placeholder={placeholder}
onChange={handleChange}
onBlur={handlerBlur}
/>
{touched[name] && errors[name] && <div className="error">{errors[name]}</div>}
</div>
)
FormikFactory(options)
not implemented yet
import { Formik, InjectedFormikProps } from 'formik/next';
const withFormik = FormikFactory<InjectedFormikProps<Props, Values>>({ ... })
const Form = props => (
<form onSubmit={props.handleSubmit}>
{/* same as usual */}
</form>
)
export default withFormik(Form)
Authors
MIT License.