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

Compatibility with StrictMode #1324

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
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
45 changes: 24 additions & 21 deletions packages/uniforms/src/BaseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ export class BaseForm<
if (this.delayId) {
clearTimeout(this.delayId);
}

// There are at least 4 places where we'd need to check, whether or not we
// actually perform `setState` after the component gets unmounted. Instead,
// we override it to hide the React warning. Also because React no longer
// will raise it in the newer versions.
// https://github.com/facebook/react/pull/22114
// https://github.com/vazco/uniforms/issues/1152
this.setState = () => {};
}

delayId?: ReturnType<typeof setTimeout> | undefined;
Expand Down Expand Up @@ -235,12 +227,14 @@ export class BaseForm<
this.delayId = setTimeout(() => {
// ...and wait for all scheduled `setState`s to commit. This is required
// for AutoForm to validate correct model, waiting in `onChange`.
this.setState(
() => null,
() => {
this.onSubmit();
},
);
if (this.mounted) {
this.setState(
() => null,
() => {
this.onSubmit();
},
);
}
}, this.props.autosaveDelay);
}
}
Expand All @@ -256,10 +250,12 @@ export class BaseForm<
}

onReset() {
// @ts-expect-error
// It's bound in constructor.
// eslint-disable-next-line @typescript-eslint/unbound-method
this.setState(this.__reset);
if (this.mounted) {
// @ts-expect-error
// It's bound in constructor.
// eslint-disable-next-line @typescript-eslint/unbound-method
this.setState(this.__reset);
}
}

onSubmit(event?: SyntheticEvent) {
Expand All @@ -268,16 +264,23 @@ export class BaseForm<
event.stopPropagation();
}

this.setState(state => (state.submitted ? null : { submitted: true }));
if (this.mounted) {
this.setState(state => (state.submitted ? null : { submitted: true }));
}

const result = this.props.onSubmit(this.getModel('submit'));
if (!(result instanceof Promise)) {
return Promise.resolve();
}

this.setState({ submitting: true });
if (this.mounted) {
this.setState({ submitting: true });
}

return result.finally(() => {
this.setState({ submitting: false });
if (this.mounted) {
this.setState({ submitting: false });
}
});
}

Expand Down