CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/lib/hooks/mounted.ts
Views: 687
1
import { useEffect, useRef } from "react";
2
3
// A *ref* that is true after component mounts, then false once
4
// the component unmounts. This obviously must be a ref since
5
// it makes no sense for unmounting or mounting to trigger
6
// a render cycle!. I made this up myself, but it does turn out
7
// to be identical to an npm package to do this:
8
// https://github.com/jmlweb/isMounted/blob/master/index.js
9
10
export default function useIsMounted() {
11
const is_mounted_ref = useRef<boolean>(false);
12
useEffect(() => {
13
is_mounted_ref.current = true;
14
return () => {
15
is_mounted_ref.current = false;
16
};
17
}, []);
18
return is_mounted_ref;
19
}
20
21