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