formik: [V2] useFormikContext doesn't return valitationSchema as expected

πŸ› Bug report

The documentation says that the returned value from useFormikContext is the same as the formik props of connect, that includes validationSchema. Also the type definition asserts that useFormikContext return value includes validationSchema, but in effect it currently doesn`t.

Current Behavior

useFormikContext return value does`t includs validationSchema

Reproducible example

Iinclude the following test in the source code

import * as React from 'react';
import { render } from 'react-testing-library';
import { Formik } from '../src/Formik';
import { useFormikContext } from '../src/FormikContext';

describe('FormikContext', () => {
  describe('useFormikContext', () => {
    it('should return validationContext if set', () => {
      const validationSchema = 'validationSchema';

      const AComponenent: React.FC = () => {
        const formikContext = useFormikContext();
        expect(formikContext.validationSchema).toBe(validationSchema);
        return null;
      };

      render(
        <Formik
          initialValues={{ test: '' }}
          validationSchema={validationSchema}
          onSubmit={() => {}}
        >
          <AComponenent />
        </Formik>
      );
    });
  });
});

The test fails

Expected behavior

The test should pass.

Suggested solution(s)

PR https://github.com/jaredpalmer/formik/pull/2090 fixes the issue

Your environment

Software Version(s)
Formik 2.06
React 16,9,2
TypeScript 3.7.2
Browser Firefox
Operating System Windows 10

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 21
  • Comments: 17 (1 by maintainers)

Most upvoted comments

+1 Same is for connect() - not returning validationSchema too! It was important for me to mark required fields with hint, depending on schema - now i can’t find any way to get it!

No clue; I opened the above PR, but I am not a maintainer.

This is resolved (I think) in the v3 PR #3231 using the separate hook useFormikConfig().

useFormikContext in the v3 PR is meant to be completely stable (it does not cause re-renders). The config itself like validate or validationSchema are often unmemoized, so in order to maintain stability in the Formik Context hook for optimization, we need to split these props out into a separate hook.

Probably safe to say it won’t be merged in given the PR was 2 years ago

I moved to React Hook Form

Actually my bad, I realized that I should not combine Formik and useFormik. Using Formik fixed that issue with empty error object.