-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Formik enters into an endless validation loop when validateOnMount is activated and initialValues change #3348
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
Comments
Ran exactly into the same issue... 😉 |
@kevinlandsberg @rapzo same issue! couldn't figure it out for a while, did you guys find any good solutions? thanks! |
if you change values, by updating initialValue, you will need to set another property to formik |
There is a bug that occurs when the initial values is constructed directly inside the parameter object of the use formik function. It recreates the object every time it validates, and every time it creates an object it validates again. The solution was to create this initial values object inside a useEffect. |
This worked! Thanks @ktalalykin . |
Thank you @ktalalykin ! Fixed the issue |
In my case @LeonardoDaLuz 's was the right approach, memoizing the initialData made more sense than enabling a reinitialization setting. Thanks |
Bug report
Current Behavior
When using Formik with
validateOnMount: true
and mutatedinitialValues
controlled outside the form context, on the 1st change, Formik will go into an infinite validation loop, crashing the page eventually.This behavior is easily caught when using a toggle in the form that affects its validation and/or structure.
Expected behavior
It is expected that, whenever
initialValues
change, a proper validation is done on the newly provided values.Reproducible example
Formik validateOnMount with a toggle that freezes the app: https://codesandbox.io/s/github/rapzo/formik-validateOnMount/tree/csb-dengj
A possible workaround, although it kind of limits the depth of the changeable value: https://codesandbox.io/s/github/rapzo/formik-validateOnMount/tree/main
Suggested solution(s)
master...rapzo:patch-1
Additional context
I don't know if this was an expected behavior so I'm creating the issue before a PR.
Will be happy to add test cases to the PR if this proposal makes sense.
Your environment
The text was updated successfully, but these errors were encountered: