formik icon indicating copy to clipboard operation
formik copied to clipboard

[V2] useFormikContext doesn't return valitationSchema as expected

Open ldicocco opened this issue 5 years ago • 17 comments

🐛 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

ldicocco avatar Dec 06 '19 21:12 ldicocco

+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!

egoreburial avatar Feb 19 '20 12:02 egoreburial

+1 for connect() too, to put asterik on required fields

Grandnainconnu avatar Aug 17 '20 05:08 Grandnainconnu

@jaredpalmer it appears as though resolving this would give back a very simple workaround for https://github.com/formium/formik/issues/1241 and https://github.com/formium/formik/issues/712

Would you accept a PR on this, or is it all hands on v3 for now?

kylemh avatar Dec 04 '20 07:12 kylemh

Hello! Any news on this? Thanks!

raphaelpc avatar Jun 18 '21 21:06 raphaelpc

Hi there,

I connect my component but when checking for errors, it always returns an empty object. Same goes with useFormikContext.

const MySchema = Yup.object().shape({
 username: Yup.string().trim().required(`Cannot be left blank`),
});

const MyForm = () => {
  const formik = useFormik({
    initialValues: {
      username: '',
    },
    validationSchema: MySchema,
    ...
  });
  
  return <Formik {...formik}>
    <Form>
      <MyComponent />
    </Form>
  </Formik>
}
const MyComponent = ({ formik: { errors }}) => {
  console.log('xxx', errors);
  return <input id="username" name="username" ..../>;
}
export default connect(MyComponent)

However, when I console.log(formik), it does return errors. Last but not least, when I type in a username and console log again formik, the errors still contain an error message regarding the username field...

Not sure what I am missing here...

Thanks any help!

"yup": "^0.32.9"
"formik": "^2.2.9",

kevinolivar avatar Jun 18 '21 23:06 kevinolivar

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

kevinolivar avatar Jun 19 '21 20:06 kevinolivar

Hi, any workarounds for this? I also need it to mark required fields with asterisk in custom form fields with formik

sudo-vaibhav avatar Oct 07 '21 09:10 sudo-vaibhav

Any update or workaround for this issue? Our current setup does not allow for prop drilling.

bombillazo avatar Feb 09 '22 22:02 bombillazo

Well, the Formik Docs say the context returns FormikProps which should include the validate and validationSchema props, but validate and validationSchema to are not passed to the ctx object created by the component:

https://github.com/jaredpalmer/formik/blob/e677bea8181f40e6762fc7e7fb009122384500c6/packages/formik/src/Formik.tsx#L949-L987

Its a question of passing the props to the context object:

     validateOnChange, 
     validateOnMount, 
     validate: props.validate,
     validationSchema: props.validationSchema
   }; 
  
   return ctx; 
 } 

bombillazo avatar Feb 09 '22 23:02 bombillazo

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.

johnrom avatar Feb 10 '22 17:02 johnrom

Oooh! Glad to know v3 is set to fix this. Do you know an ETA for v3?

bombillazo avatar Feb 10 '22 17:02 bombillazo

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

johnrom avatar Feb 10 '22 17:02 johnrom

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

crhistianramirez avatar Mar 20 '23 18:03 crhistianramirez

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

I moved to React Hook Form

bombillazo avatar Mar 24 '23 15:03 bombillazo

I did as well, very similar API and a lot more performant but I'm noticing they are missing the same feature, although it looks like there was discussion on it in the past month so that's promising. If you're interested in having them work on it you should upvote the post, along with the related issue. That seems to be the main way they prioritize work

crhistianramirez avatar Mar 24 '23 16:03 crhistianramirez

I'm getting re-render loops when using useFormikContext. Is the values prop not "stable"? I'm using values as the dependency for useEffect and it's getting triggered even though the values aren't changing.

splurgebudget avatar Sep 30 '23 16:09 splurgebudget

Never mind - as per usual, it was my own fault that had nothing to do with Formik. Carry on.

splurgebudget avatar Sep 30 '23 16:09 splurgebudget