Created
December 10, 2021 22:53
-
-
Save JacobJaffe/fe748746126b612a895e71a71f191fa2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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"; | |
/** | |
* Returns a boolean ref to determine if the component is still mounted. | |
* Useful for (stopping) async logic that can't occur after a dismount. | |
*/ | |
export const useIsMounted = () => { | |
const mounted = useRef(true); | |
useEffect( | |
() => () => { | |
mounted.current = false; | |
}, | |
[] | |
); | |
return mounted; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment