Skip to content

Instantly share code, notes, and snippets.

@JacobJaffe
Created December 10, 2021 22:53
Show Gist options
  • Save JacobJaffe/fe748746126b612a895e71a71f191fa2 to your computer and use it in GitHub Desktop.
Save JacobJaffe/fe748746126b612a895e71a71f191fa2 to your computer and use it in GitHub Desktop.
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