Skip to content

Commit

Permalink
Merge pull request #1 from vazco/fix-strict-mode
Browse files Browse the repository at this point in the history
Fix strict mode
  • Loading branch information
aeciorc authored Apr 30, 2024
2 parents 4b3f109 + e76fe3d commit aa4e9e7
Showing 1 changed file with 24 additions and 21 deletions.
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

0 comments on commit aa4e9e7

Please sign in to comment.