Skip to content

Commit 093e5f6

Browse files
asazernikerikras
authored andcommitted
RFC: pausing validation before unmount (#595)
* no-op, ReactFinalForm.js: More commenting for pause/resumeValidation() on init * ReactFinalForm.js: Pause validation before unmount This was causing serious performance problems on large forms, since validation is generally O(number_fields), and we'd be calling it number_fields times when running all the deregistrations.
1 parent 4eb0d7c commit 093e5f6

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/ReactFinalForm.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({
6767

6868
const form: FormApi<FormValues> = useConstant(() => {
6969
const f = alternateFormApi || createForm<FormValues>(config)
70+
// pause validation until children register all fields on first render (unpaused in useEffect() below)
7071
f.pauseValidation()
7172
return f
7273
})
@@ -87,7 +88,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({
8788
const stateRef = useLatest<FormState<FormValues>>(state)
8889

8990
React.useEffect(() => {
90-
// We have rendered, so all fields are no registered, so we can unpause validation
91+
// We have rendered, so all fields are now registered, so we can unpause validation
9192
form.isValidationPaused() && form.resumeValidation()
9293
const unsubscriptions: Unsubscribe[] = [
9394
form.subscribe(s => {
@@ -105,7 +106,9 @@ function ReactFinalForm<FormValues: FormValuesShape>({
105106
]
106107

107108
return () => {
109+
form.pauseValidation() // pause validation so we don't revalidate on every field deregistration
108110
unsubscriptions.forEach(unsubscribe => unsubscribe())
111+
// don't need to resume validation here; either unmounting, or will re-run this hook with new deps
109112
}
110113
// eslint-disable-next-line react-hooks/exhaustive-deps
111114
}, [decorators])

0 commit comments

Comments
 (0)