From 536d242ac9ed65a7be3f453f6dd3fab0037b05b9 Mon Sep 17 00:00:00 2001 From: Aries Date: Mon, 26 Aug 2024 15:40:33 +0800 Subject: [PATCH 1/2] fix memory leak warning, clear timer when the component unmounts --- frontend/src/components/toast/toast.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/toast/toast.js b/frontend/src/components/toast/toast.js index 0cfc681c4ef..e0a7395cb1e 100644 --- a/frontend/src/components/toast/toast.js +++ b/frontend/src/components/toast/toast.js @@ -6,6 +6,8 @@ import Alert from './alert'; const ANIMATION_DURATION = 240; export default class Toast extends React.PureComponent { + _isMounted = false; + static propTypes = { /** * The z-index of the toast. @@ -67,10 +69,12 @@ export default class Toast extends React.PureComponent { } componentDidMount() { + this._isMounted = true; this.startCloseTimer(); } componentWillUnmount() { + this._isMounted = false; this.clearCloseTimer(); } @@ -80,15 +84,19 @@ export default class Toast extends React.PureComponent { event.stopPropagation(); } this.clearCloseTimer(); - this.setState({ - isShown: false - }); + if (this._isMounted) { + this.setState({ + isShown: false + }); + } }; startCloseTimer = () => { if (this.props.duration) { this.closeTimer = setTimeout(() => { - this.close(); + if (this._isMounted) { + this.close(); + } }, this.props.duration * 1000); } }; From 48fb8e39dfaa09481a7474ada16ef8ae19e1a2e4 Mon Sep 17 00:00:00 2001 From: Aries Date: Mon, 26 Aug 2024 18:19:16 +0800 Subject: [PATCH 2/2] clean up redundant check --- frontend/src/components/toast/toast.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/frontend/src/components/toast/toast.js b/frontend/src/components/toast/toast.js index e0a7395cb1e..770ccb2f15d 100644 --- a/frontend/src/components/toast/toast.js +++ b/frontend/src/components/toast/toast.js @@ -94,9 +94,7 @@ export default class Toast extends React.PureComponent { startCloseTimer = () => { if (this.props.duration) { this.closeTimer = setTimeout(() => { - if (this._isMounted) { - this.close(); - } + this.close(); }, this.props.duration * 1000); } };