Created
May 8, 2019 05:38
-
-
Save gaperton/b74f0d81850491a9cb9bd14a007f76f8 to your computer and use it in GitHub Desktop.
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
export function useIsMountedRef(){ | |
// We need something similar to the plain mutable class member. | |
const isMounted = useRef( true ); | |
// And, we need something similar to componentWillUnmount. | |
useEffect( () => { | |
// Whatever we return is a cleanup effect. | |
return () => { // <- componentWillUnmount | |
isMounted.current = false | |
} | |
}, []); // [] never changes, so the "cleanup" function will be fired on unmount only. | |
return isMounted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment