Created
January 16, 2020 21:22
-
-
Save bartcis/56889ba108af941fc9e5c2fceb372329 to your computer and use it in GitHub Desktop.
useEffect cleanup example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [showLoading, setShowLoading] = useState(false) | |
useEffect( | |
() => { | |
let timer1 = setTimeout(() => setShowLoading(true), 1000) | |
// this will clear Timeout when component unmont like in willComponentUnmount | |
return () => { | |
clearTimeout(timer1) | |
} | |
}, | |
[] //useEffect will run only one time | |
//if you pass a value to array, like this [data] than clearTimeout | |
//will run every time this value changes (useEffect re-run) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment