Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
34 lines
808 B
Plaintext
34 lines
808 B
Plaintext
// src/index.ts
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
function useIsMounted(props = {}) {
|
|
const { rerender = false, delay = 0 } = props;
|
|
const isMountedRef = useRef(false);
|
|
const [isMounted, setIsMounted] = useState(false);
|
|
useEffect(() => {
|
|
isMountedRef.current = true;
|
|
let timer = null;
|
|
if (rerender) {
|
|
if (delay > 0) {
|
|
timer = setTimeout(() => {
|
|
setIsMounted(true);
|
|
}, delay);
|
|
} else {
|
|
setIsMounted(true);
|
|
}
|
|
}
|
|
return () => {
|
|
isMountedRef.current = false;
|
|
if (rerender) {
|
|
setIsMounted(false);
|
|
}
|
|
if (timer) {
|
|
clearTimeout(timer);
|
|
}
|
|
};
|
|
}, [rerender]);
|
|
return [useCallback(() => isMountedRef.current, []), isMounted];
|
|
}
|
|
export {
|
|
useIsMounted
|
|
};
|