-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
Bug report
Current Behavior
When navigating to a page where Formik is not required, components that rely on Formik context (e.g., <Field>, <Form>, <ErrorMessage>) throw errors because FormikContext is undefined. This issue arises in Formik v2.4.x due to the change in how context is initialized.
Expected behavior
Components that consume Formik context should fail gracefully or provide meaningful warnings when rendered outside a <Formik> provider. Ideally, developers shouldn't have to manually add safe checks everywhere.
Reproducible example
You can reproduce this issue by rendering a Formik-connected component (like <Field>) on a page that does not wrap it in a <Formik> provider. This was not an issue in Formik v1.x.
Formik v1.x:
const FormikContext = createContext({});
Formik v2.4.x:
const FormikContext = React.createContext(undefined);
Suggested solution(s)
- Revert to using a default empty object (
{}) increateContext()to avoid runtime errors. - Provide a utility or wrapper to safely consume Formik context without requiring manual checks.
Additional context
This change in context initialization forces developers to add null checks or conditional rendering logic around every Formik-connected component, which adds unnecessary boilerplate and increases the risk of runtime errors.
Your environment
| Software | Version(s) |
|---|---|
| Formik | 2.4.x |
| React | 18.0+ |
| Browser | Chrome, Firefox |
| npm/Yarn | Latest |
| Operating System | macOS, Windows |
.