From de646940026bcec90290d970f061d575c80b7b4e Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Wed, 5 Feb 2020 10:05:32 +0800 Subject: [PATCH] clear timeout on unmount --- resources/assets/src/components/Toast.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/resources/assets/src/components/Toast.tsx b/resources/assets/src/components/Toast.tsx index 9c6f7524..e08b8db3 100644 --- a/resources/assets/src/components/Toast.tsx +++ b/resources/assets/src/components/Toast.tsx @@ -20,9 +20,15 @@ const Toast: React.FC = props => { const [show, setShow] = useState(false) useEffect(() => { - setTimeout(() => setShow(true), 100) - setTimeout(() => setShow(false), 3000) - setTimeout(props.onClose, 3100) + const id1 = setTimeout(() => setShow(true), 100) + const id2 = setTimeout(() => setShow(false), 3000) + const id3 = setTimeout(props.onClose, 3100) + + return () => { + clearTimeout(id1) + clearTimeout(id2) + clearTimeout(id3) + } }, [props.onClose]) const type = props.type === 'error' ? 'danger' : props.type