Created
June 16, 2021 07:53
-
-
Save yoavniran/f94f10c5c597e88e8cf19cf0e5e913ad to your computer and use it in GitHub Desktop.
useIsMounted React hook
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 { useEffect, useRef, useCallback } from "react"; | |
const useIsMounted = () => { | |
const mountedRef = useRef(true); | |
useEffect(() => { | |
return () => { | |
mountedRef.current = false; | |
}; | |
}, []); | |
const getIsMounted = useCallback(() => | |
mountedRef.current, []); | |
return getIsMounted; | |
}; | |
export default useIsMounted; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment