Last active
April 14, 2020 11:33
-
-
Save mic-css/6703224e66338cd72b13f9462d462753 to your computer and use it in GitHub Desktop.
Custom hook that keeps track of whether a component is mounted, useful for canceling asynchronous setStates
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
import { useRef, useEffect } from 'react' | |
export const useIsMounted = () => { | |
const isMounted = useRef(false) | |
useEffect(() => { | |
isMounted.current = true | |
return () => { | |
isMounted.current = false | |
} | |
}, []) | |
return isMounted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment