formik provider

Understanding Formik Provider in React

If you have worked with React, then you must be aware of the fact that managing a form state can be quite challenging. This is where Formik comes into play. Formik is a popular form management library in React that makes it easy to handle form states and validations.

Formik provides a FormikProvider component that acts as a bridge between the Formik context and the form components. It also provides the necessary helpers and utilities required for managing the form's state.

Using Formik Provider

Let's see how to use the FormikProvider in a simple form:


import { Formik, Form, Field } from 'formik';
import { FormikProvider } from 'formik';

function MyForm() {
  return (
    <Formik
      initialValues={{ name: '', email: '' }}
      onSubmit={values => {
        console.log(values);
      }}
    >
      {() => (
        <Form>
          <FormikProvider>
            <label htmlFor="name">Name</label>
            <Field id="name" name="name" />

            <label htmlFor="email">Email</label>
            <Field id="email" name="email" type="email" />

            <button type="submit">Submit</button>
          </FormikProvider>
        </Form>
      )}
    </Formik>
  );
}

In the above example, we are using the FormikProvider component inside the Formik component. It provides access to all the Formik context values and functions to the form components.

FormikProvider Props

The FormikProvider component accepts the following props:

  • children: This prop is used to wrap the form components that need to access the Formik context.

Using Multiple Formik Providers

Sometimes, we may need multiple Formik providers in a form when we have nested components that require different contexts. In such cases, we can use the useFormikContext hook to access the parent context values.


import { Formik, Form, Field, useFormikContext } from 'formik';
import { FormikProvider } from 'formik';

function MyForm() {
  return (
    <Formik
      initialValues={{ name: '', email: '' }}
      onSubmit={values => {
        console.log(values);
      }}
    >
      {() => (
        <Form>
          <FormikProvider>
            <label htmlFor="name">Name</label>
            <Field id="name" name="name" />

            <label htmlFor="email">Email</label>
            <Field id="email" name="email" type="email" />

            <FormikProvider>
              <MyNestedComponent />
            </FormikProvider>

            <button type="submit">Submit</button>
          </FormikProvider>
        </Form>
      )}
    </Formik>
  );
}

function MyNestedComponent() {
  const { values } = useFormikContext();

  return (
    <div>
      <p>Name: {values.name}</p>
      <p>Email: {values.email}</p>
    </div>
  );
}

In the above example, we have a nested component that requires access to the parent Formik context. We are using the useFormikContext hook to access the values from the parent context.

Conclusion

FormikProvider is an essential component of the Formik library that provides access to the Formik context and its utilities. It makes it easy to manage form state and validations in React applications.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe