Skip to content

Commit

Permalink
Compatibility with StrictMode (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpospiech committed May 24, 2024
1 parent 6aace6c commit e7d7785
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 @@ -93,14 +93,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 @@ -237,12 +229,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 @@ -258,10 +252,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 @@ -270,16 +266,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 e7d7785

Please sign in to comment.