Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialValues is now required? #1967

Open
srowe0091 opened this issue Oct 30, 2019 · 8 comments
Open

initialValues is now required? #1967

srowe0091 opened this issue Oct 30, 2019 · 8 comments

Comments

@srowe0091
Copy link

srowe0091 commented Oct 30, 2019

🐛 Bug report

Current Behavior

A form that does NOT have initialValues, the application crashes because formik cannot read property [field name] of undefined

Expected behavior

A form that does not require fields to be initialized nor keep track of values, for example a login form. It shouldn't be required to apply the initialValues prop

Reproducible example

Here is a sandbox with the latest formik that shows the behavior. If you uncomment line 9, you will see the behavior of not applying initialValues, which causes the app to break as soon as you type into the field

https://codesandbox.io/s/formik-codesandbox-template-9rstl

Suggested solution(s)

Not making the initialValues required.

Additional context

If this is part of the migration of V1 to V2, then it should be documented because in V1, initialValues was not required

Your environment

Software Version(s)
Formik latest
React latest

Edit: fixed sandbox link

@apieceofbart
Copy link

apieceofbart commented Oct 30, 2019

From the docs:

Even if your form is empty by default, you must initialize all fields with initial values otherwise React will throw an error saying that you have changed an input from uncontrolled to controlled.

Maybe Formik could provide a default value (empty object) ?

@srowe0091
Copy link
Author

@apieceofbart I read that, but that isn't the case here. The docs say that react will throw an error if going from uncontrolled to controlled; also it wouldn't destroy the app either. I am using the forms inputs (the login form) as UNCONTROLLED. So there isn't a need to initialize the fields. Also this behavior was not present in V1 when not providing initialValues.

In the codesandbox, if you open the console, uncomment line 9, and try to type in the field, you will see the error that crashes the application

@riccoski
Copy link

riccoski commented Nov 4, 2019

Same. Have to use an empty object for initialValues

@antiold
Copy link

antiold commented Nov 14, 2019

Discover this by trying to setFieldValue, which gives a setIn error.
Should be documented that initialValues should be at least {}.

@srowe0091
Copy link
Author

@antiold this wasn't the case in V1. There seems to be a few undocumented changes in V2 that breaks implementation.

@kylepeeler
Copy link

We had the same issue, and until this was discovered, it was a blocker to us upgrading to Formik v2. Please add a default prop or make a note in the migration notes about this change. Passing undefined into here was frequently occuring when initialValues was set to a value connected to Redux.

@stale stale bot removed the stale label Jun 19, 2020
@johnrom
Copy link
Collaborator

johnrom commented Jun 19, 2020

InitialValues has always been required. Using typescript, your build will fail when not passing an object. If it were up to me, I'd throw an error in JS as well to prevent hidden errors like using setFieldValue as described above.

An empty object is not assignable to Formik's values signature, so there is no way for Formik to create a valid default object. I don't think we should document something like, "If you're going to do it wrong, at least do it wrong like this." It provides a false alternative. I think this will be resolved in v3 because initialValues will probably be optional, but if not we should just throw an error if initialValues is undefined.

Passing undefined into here was frequently occuring when initialValues was set to a value connected to Redux.

@kylepeeler Formik cannot determine what your fallback object should be. You should provide your own fallback when Redux is not populated. It should also match the expected values signature.

const MyForm = () => {
  const defaultValues = React.useMemo(() => selectDefaultValues(), []);

  return <Formik
    initialValues={valueFromRedux ?? defaultValues}
    enableReinitialize={true}
  />
}

@TimMTech
Copy link

TimMTech commented Oct 6, 2022

My advice is to avoid these bullshit, over engineered libraries and just one your self in 1/4 the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants