Skip to content

Instantly share code, notes, and snippets.

@enqtran
Created November 7, 2021 14:14
Show Gist options
  • Save enqtran/4cefb87b2560ca6d467b0e6c837ba959 to your computer and use it in GitHub Desktop.
Save enqtran/4cefb87b2560ca6d467b0e6c837ba959 to your computer and use it in GitHub Desktop.
import { useEffect, useRef } from "react"
/*
isMounted = useIsMounted()
useEffect(() => {
if (isMounted.current) {
// code...
}
}, [isMounted])
*/
const useIsMounted = () => {
const isMounted = useRef(null)
useEffect(() => {
isMounted.current = true
return () => {
isMounted.current = false
}
})
return isMounted
}
export default useIsMounted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment