From 2e85559dfe97a68c686ab59e17a1b0f2a52c6e7a Mon Sep 17 00:00:00 2001 From: Aries Date: Mon, 26 Aug 2024 18:22:27 +0800 Subject: [PATCH] fix memory leak warning, clear timer when the component unmounts (#6641) * fix memory leak warning, clear timer when the component unmounts * clean up redundant check --- frontend/src/components/toast/toast.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/toast/toast.js b/frontend/src/components/toast/toast.js index 0cfc681c4e..770ccb2f15 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,9 +84,11 @@ export default class Toast extends React.PureComponent { event.stopPropagation(); } this.clearCloseTimer(); - this.setState({ - isShown: false - }); + if (this._isMounted) { + this.setState({ + isShown: false + }); + } }; startCloseTimer = () => {