File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change 11import { useEffect , useRef } from 'react' ;
22
3+ /**
4+ * Returns mount status of component. This hook can be used for purpose of avoiding any
5+ * setState calls (within async operation) after component gets unmounted.
6+ *
7+ * @example
8+ * ```
9+ * const isMounted = useIsMountedRef();
10+ * const [dummyValue, setDummyValue] = useState(false);
11+ *
12+ * useEffect(() => {
13+ * someAsyncOperation().then(() => {
14+ * if (isMounted.current) setDummyValue(true);
15+ * })
16+ * })
17+ * ```
18+ *
19+ * @returns isMounted {Object} Mount status ref for the component.
20+ */
321export const useIsMountedRef = ( ) => {
422 // Initial value has been set to true, since this hook exist only for sole purpose of
523 // avoiding any setState calls (within async operation) after component gets unmounted.
You can’t perform that action at this time.
0 commit comments