Skip to content

Instantly share code, notes, and snippets.

@gaperton
Created May 8, 2019 05:38
Show Gist options
  • Save gaperton/b74f0d81850491a9cb9bd14a007f76f8 to your computer and use it in GitHub Desktop.
Save gaperton/b74f0d81850491a9cb9bd14a007f76f8 to your computer and use it in GitHub Desktop.
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