Skip to content

Instantly share code, notes, and snippets.

@bartcis
Created January 16, 2020 21:22
Show Gist options
  • Save bartcis/56889ba108af941fc9e5c2fceb372329 to your computer and use it in GitHub Desktop.
Save bartcis/56889ba108af941fc9e5c2fceb372329 to your computer and use it in GitHub Desktop.
useEffect cleanup example
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