Created
February 27, 2020 15:07
-
-
Save scorsi/5fd57ba892c5bf11e87f07b0e2286af6 to your computer and use it in GitHub Desktop.
A hook useful to know if the current component is still mounted
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 } from 'react'; | |
export default () => { | |
const isMounted = useRef(null); | |
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