Merge pull request #6837 from allenve/master

fix: tpl组件销毁时取消setState
This commit is contained in:
Allen 2023-05-10 18:40:49 +08:00 committed by GitHub
commit a4aed820ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,12 +63,14 @@ export class Tpl extends React.Component<TplProps, TplState> {
}; };
dom: any; dom: any;
mounted: boolean;
constructor(props: TplProps) { constructor(props: TplProps) {
super(props); super(props);
this.state = { this.state = {
content: this.getContent() content: this.getContent()
}; };
this.mounted = true;
} }
componentDidUpdate(prevProps: Readonly<TplProps>): void { componentDidUpdate(prevProps: Readonly<TplProps>): void {
@ -85,13 +87,17 @@ export class Tpl extends React.Component<TplProps, TplState> {
this.updateContent(); this.updateContent();
} }
componentWillUnmount() {
this.mounted = false;
}
@autobind @autobind
updateContent() { updateContent() {
const {tpl, html, text} = this.props; const {tpl, html, text} = this.props;
if (html || tpl || text) { if (html || tpl || text) {
this.getAsyncContent().then(content => { this.getAsyncContent().then(content => {
this.setState({content}); this.mounted && this.setState({content});
}); });
} }
} }