Skip to content

RFC: pausing validation before unmount #595

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

Merged
merged 2 commits into from
Aug 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ReactFinalForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({

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

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

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