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

Batch async state updates #2006

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 38 additions & 24 deletions src/Formik.tsx
Expand Up @@ -22,6 +22,7 @@ import {
getActiveElement,
getIn,
isObject,
batch,
} from './utils';
import { FormikProvider } from './FormikContext';
import invariant from 'tiny-warning';
Expand Down Expand Up @@ -323,9 +324,11 @@ export function useFormik<Values extends FormikValues = FormikValues>({
(values: Values = state.values) => {
return unstable_runWithPriority(LowPriority, () => {
return runAllValidations(values).then(combinedErrors => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ERRORS', payload: combinedErrors });
}
batch(() => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ERRORS', payload: combinedErrors });
}
});
return combinedErrors;
});
});
Expand All @@ -337,12 +340,14 @@ export function useFormik<Values extends FormikValues = FormikValues>({
(values: Values = state.values) => {
dispatch({ type: 'SET_ISVALIDATING', payload: true });
return runAllValidations(values).then(combinedErrors => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ISVALIDATING', payload: false });
if (!isEqual(state.errors, combinedErrors)) {
dispatch({ type: 'SET_ERRORS', payload: combinedErrors });
batch(() => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ISVALIDATING', payload: false });
if (!isEqual(state.errors, combinedErrors)) {
dispatch({ type: 'SET_ERRORS', payload: combinedErrors });
}
}
}
});
return combinedErrors;
});
}
Expand Down Expand Up @@ -471,11 +476,13 @@ export function useFormik<Values extends FormikValues = FormikValues>({
return maybePromise
.then((x: any) => x)
.then((error: string) => {
dispatch({
type: 'SET_FIELD_ERROR',
payload: { field: name, value: error },
batch(() => {
dispatch({
type: 'SET_FIELD_ERROR',
payload: { field: name, value: error },
});
dispatch({ type: 'SET_ISVALIDATING', payload: false });
});
dispatch({ type: 'SET_ISVALIDATING', payload: false });
});
} else {
dispatch({
Expand Down Expand Up @@ -712,22 +719,29 @@ export function useFormik<Values extends FormikValues = FormikValues>({
if (isActuallyValid) {
return Promise.resolve(executeSubmit())
.then(() => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_SUCCESS' });
}
batch(() => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_SUCCESS' });
}
});
})
.catch(_errors => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
throw _errors;
}
batch(() => {
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
throw _errors;
}
});
});
} else if (!!isMounted.current) {
// ^^^ Make sure Formik is still mounted before calling setState
dispatch({ type: 'SUBMIT_FAILURE' });
} else {
batch(() => {
// ^^^ Make sure Formik is still mounted before calling setState
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
}
});
return;
}
return;
}
);
});
Expand Down Expand Up @@ -780,7 +794,7 @@ export function useFormik<Values extends FormikValues = FormikValues>({
);

if (isPromise(maybePromisedOnReset)) {
(maybePromisedOnReset as Promise<any>).then(resetForm);
(maybePromisedOnReset as Promise<any>).then(() => batch(resetForm));
} else {
resetForm();
}
Expand Down
30 changes: 30 additions & 0 deletions src/utils.ts
Expand Up @@ -169,3 +169,33 @@ export function setNestedObjectValues<T>(

return response;
}

export type Batch = (cb: Function) => void;
jaredpalmer marked this conversation as resolved.
Show resolved Hide resolved

function getBatchedUpdates(): Batch {
let renderer: {
unstable_batchedUpdates?: Batch;
batchedUpdates?: Batch;
};

try {
renderer = require('react-dom');
} catch {
try {
renderer = require('react-native');
} catch {
renderer = {};
}
}

const {
unstable_batchedUpdates = (cb: Function) => {
cb();
},
batchedUpdates = unstable_batchedUpdates,
} = renderer;

return batchedUpdates;
}

export const batch = getBatchedUpdates();