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

FieldArray runs UI-blocking "validateFormWithHighPriority" on every change #2542

Open
MaximSagan opened this issue Jun 22, 2020 · 3 comments

Comments

@MaximSagan
Copy link

MaximSagan commented Jun 22, 2020

🐛 Bug report

Current Behavior

Internally, Formik has two different functions to trigger validation, validateFormWithLowPriority and validateFormWithHighPriority.

  • validateFormWithLowPriority is used internally on setValues, setFieldValue, setTouched, setFieldTouched, and validateOnMount.

  • validateFormWithHighPriority is

This publicly available validateForm (alias of validateFormWithHighPriority) which is part of Formik context is called in one location by Formik itself: inside FieldArray's componentDidUpdate method here. validateForm is run whenever the target array of FieldArray is updated (or more specificially when its reference is updated) as long as validateOnChange is true (which is the default setting).

We can see how often it gets called by looking at the following sandbox, where I am logging each time validateForm is called:

https://codesandbox.io/s/formik-fieldarray-high-priority-validation-v598j

Note how validateForm (alias for validateFormWithHighPriority) is called any time fields declared inside FieldArray array update, but not when the field outside the form is updated.

This call to validateWithHighPriority blocks the UI thread and can be extremely detrimental to the performance of larger forms. I should probably investigate this further, but I'm not quite sure why this needs to be called if updateValues/updateFieldValue already run validation (albeit with "low priority").

Expected behavior

The use of FieldArray should not cause UI-blocking validation on every keystroke.

Reproducible example

https://codesandbox.io/s/formik-fieldarray-high-priority-validation-v598j (referred to above)

Suggested solution(s)

Remove this explicit call by FieldArray to validateForm if unnecessary, or perhaps replace it with low-priority validation if necessary.

Your environment

See CodeSandbox example.

@darbio
Copy link

darbio commented Dec 28, 2020

Is there a workaround for this? I'm facing issues with a really simple (two repeating values) form which makes the UX terrible.

@lukasrakauskas
Copy link

Is there a workaround for this? I'm facing issues with a really simple (two repeating values) form which makes the UX terrible.

For me, setting validateOnChange to false brought acceptable level of performance back.

@gwooly
Copy link

gwooly commented Nov 30, 2021

@lukasrakauskas suggestion seems to solve the input delay. Even if you manually call validateField or validateForm each time one of the field array values changes (or you add / remove items from the field array) then it's far more performant.

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

4 participants