Skip to content
the poor coder

formik provider

Poor Man
Published
Reading time
2 min read
On this page

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:

JavaScript

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.

JavaScript

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC