clear timeout on unmount

This commit is contained in:
Pig Fang 2020-02-05 10:05:32 +08:00
parent 5917e62286
commit de64694002

View File

@ -20,9 +20,15 @@ const Toast: React.FC<Props> = props => {
const [show, setShow] = useState(false) const [show, setShow] = useState(false)
useEffect(() => { useEffect(() => {
setTimeout(() => setShow(true), 100) const id1 = setTimeout(() => setShow(true), 100)
setTimeout(() => setShow(false), 3000) const id2 = setTimeout(() => setShow(false), 3000)
setTimeout(props.onClose, 3100) const id3 = setTimeout(props.onClose, 3100)
return () => {
clearTimeout(id1)
clearTimeout(id2)
clearTimeout(id3)
}
}, [props.onClose]) }, [props.onClose])
const type = props.type === 'error' ? 'danger' : props.type const type = props.type === 'error' ? 'danger' : props.type