Installation
Props
Prop | Type | Default | Description |
---|---|---|---|
config | FormikConfig<T> | - | Configuration object passed to Formik |
See useFormik for more details about the configuration options.
Data
Property | Type | Description |
---|---|---|
onSubmit | (handler) => (e?) => void | Simplified and memoized submit handler |
values | T | Current form values |
errors | FormikErrors<T> | Form validation errors |
touched | FormikTouched<T> | Fields that have been touched |
...formik | FormikProps<T> | All methods returned by useFormik (handleChange, reset, etc.) |
Key Features & Details
- Built on Formik – see Formik Docs for full API.
- Simplified
onSubmit
signature that handles form submission automatically. - Memoized handler with
useCallback
to prevent unnecessary re-renders. - Exposes all
useFormik
methods (handleChange
,resetForm
,setFieldValue
, etc.). - Optional debug logging of
values
anderrors
changes in development mode. - Automatic form event handling with
preventDefault()
.
Common Use Cases
- Single-field forms with basic validation (e.g., contact forms, search).
- Multi-field forms with initial values and complex validation.
- Forms requiring field-level validation and error display.
- Dynamic forms with conditional fields and validation.
- Rapid prototyping of form-driven UIs with Formik's ecosystem.
Examples
Basic Usage
const { values, errors, touched, handleChange, handleBlur, onSubmit } =
useFormikHook<{
firstName: string;
}>({
initialValues: { firstName: '' },
validate: (values) => {
const errors: any = {};
if (!values.firstName) {
errors.firstName = 'First name is required';
}
return errors;
},
onSubmit: () => {}, // Will be overridden by onSubmit handler
});
return (
<form onSubmit={onSubmit((data) => alert(`Hello, ${data.firstName}!`))}>
<div>
<label htmlFor="firstName">First Name</label>
<input
id="firstName"
name="firstName"
value={values.firstName}
onChange={handleChange}
onBlur={handleBlur}
/>
{touched.firstName && errors.firstName && <span>{errors.firstName}</span>}
</div>
<button type="submit">Submit</button>
</form>
);
Handling Validation with Yup
import * as Yup from 'yup';
const validationSchema = Yup.object({
email: Yup.string().email('Invalid email').required('Email is required'),
password: Yup.string()
.min(6, 'Password must be at least 6 characters')
.required('Password is required'),
});
const { values, errors, touched, handleChange, handleBlur, onSubmit } =
useFormikHook({
initialValues: { email: '', password: '' },
validationSchema,
onSubmit: () => {}, // Will be overridden
});
const handleFormSubmit = (data, { setSubmitting }) => {
console.log('Form Data:', data);
setSubmitting(false);
};
return (
<form onSubmit={onSubmit(handleFormSubmit)}>
<div>
<label htmlFor="email">Email</label>
<input
id="email"
name="email"
type="email"
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
/>
{touched.email && errors.email && <span>{errors.email}</span>}
</div>
<div>
<label htmlFor="password">Password</label>
<input
id="password"
name="password"
type="password"
value={values.password}
onChange={handleChange}
onBlur={handleBlur}
/>
{touched.password && errors.password && <span>{errors.password}</span>}
</div>
<button type="submit">Submit</button>
</form>
);
Resetting the Form
const { values, handleChange, onSubmit, resetForm } = useFormikHook<{
firstName: string;
}>({
initialValues: { firstName: 'John' },
onSubmit: () => {},
});
const handleFormSubmit = (data) => {
console.log('Submitted:', data);
resetForm({ values: { firstName: '' } });
};
return (
<form onSubmit={onSubmit(handleFormSubmit)}>
<div>
<label htmlFor="firstName">First Name</label>
<input
id="firstName"
name="firstName"
value={values.firstName}
onChange={handleChange}
/>
</div>
<button type="submit">Submit</button>
<button type="button" onClick={() => resetForm()}>
Reset
</button>
</form>
);