Skip to content

Instantly share code, notes, and snippets.

@pavermakov
Created April 4, 2020 14:41
Show Gist options
  • Save pavermakov/c278273cbf1151518d3e7cbb01ca418d to your computer and use it in GitHub Desktop.
Save pavermakov/c278273cbf1151518d3e7cbb01ca418d to your computer and use it in GitHub Desktop.
import { useCallback, useEffect, useRef } from 'react';
export function useMounted() {
const refMounted = useRef(false);
useEffect(() => {
refMounted.current = true;
return () => {
refMounted.current = false;
};
});
const isMounted = useCallback(() => {
return refMounted.current;
}, []);
return isMounted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment