CARVIEW |
Select Language
HTTP/2 200
access-control-allow-origin: *
age: 1553837
cache-control: public, max-age=0, must-revalidate
content-disposition: inline; filename="useFormikContext"
content-encoding: gzip
content-type: text/html; charset=utf-8
date: Sun, 12 Oct 2025 06:26:27 GMT
etag: W/"79d6c029a242ec8401358978fb977f9f"
last-modified: Wed, 24 Sep 2025 06:49:09 GMT
server: Vercel
strict-transport-security: max-age=63072000
x-matched-path: /docs/api/useFormikContext
x-vercel-cache: HIT
x-vercel-id: bom1::w9bj2-1760250387670-dd5dadf0af3e
useFormikContext() | Formik
useFormikContext()
useFormikContext()
is a custom React hook that will return all Formik state and helpers via React Context.
Example
Here's an example of a form that works similarly to Stripe's 2-factor verification form. As soon as you type a 6 digit number, the form will automatically submit (i.e. no enter keypress is needed).
1 import React from 'react';2 import { useFormikContext, Formik, Form, Field } from 'formik';34 const AutoSubmitToken = () => {5 // Grab values and submitForm from context6 const { values, submitForm } = useFormikContext();7 React.useEffect(() => {8 // Submit the form imperatively as an effect as soon as form values.token are 6 digits long9 if (values.token.length === 6) {10 submitForm();11 }12 }, [values, submitForm]);13 return null;14 };1516 const TwoFactorVerificationForm = () => (17 <div>18 <h1>2-step Verification</h1>19 <p>Please enter the 6 digit code sent to your device</p>20 <Formik21 initialValues={{ token: '' }}22 validate={values => {23 const errors = {};24 if (values.token.length < 5) {25 errors.token = 'Invalid code. Too short.';26 }27 return errors;28 }}29 onSubmit={(values, actions) => {30 setTimeout(() => {31 alert(JSON.stringify(values, null, 2));32 actions.setSubmitting(false);33 }, 1000);34 }}35 >36 <Form>37 <Field name="token" type="tel" />38 <AutoSubmitToken />39 </Form>40 </Formik>41 </div>42 );
Reference
useFormikContext(): FormikProps<Values>
A custom React Hook that returns Formik states and helpers via React Context. Thus, this hook will only work if there is a parent Formik React Context from which it can pull from. If called without a parent context (i.e. a descendent of a <Formik>
component or withFormik
higher-order component), you will get a warning in your console.
Was this page helpful?
On this page
Resources
About Formium
Subscribe to our newsletter
The latest Formik news, articles, and resources, sent to your inbox.