You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
@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.
🐛 Bug report
Current Behavior
Internally, Formik has two different functions to trigger validation, validateFormWithLowPriority and validateFormWithHighPriority.
validateFormWithLowPriority
is used internally onsetValues
,setFieldValue
,setTouched
,setFieldTouched
, andvalidateOnMount
.validateFormWithHighPriority
issubmitForm
.validateForm
(exposed both as part of the imperativeMethods object that is available toonSubmit
andonReset
callbacks, and as part of the main Formik context that is received by Formik's render props and whenuseFormikContext
is called).This publicly available
validateForm
(alias ofvalidateFormWithHighPriority
) which is part of Formik context is called in one location by Formik itself: insideFieldArray
'scomponentDidUpdate
method here.validateForm
is run whenever the target array of FieldArray is updated (or more specificially when its reference is updated) as long asvalidateOnChange
istrue
(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 forvalidateFormWithHighPriority
) 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
tovalidateForm
if unnecessary, or perhaps replace it with low-priority validation if necessary.Your environment
See CodeSandbox example.
The text was updated successfully, but these errors were encountered: