Skip to content

Instantly share code, notes, and snippets.

@yoavniran
Created June 16, 2021 07:53
Show Gist options
  • Save yoavniran/f94f10c5c597e88e8cf19cf0e5e913ad to your computer and use it in GitHub Desktop.
Save yoavniran/f94f10c5c597e88e8cf19cf0e5e913ad to your computer and use it in GitHub Desktop.
useIsMounted React hook
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