@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
28 lines (24 loc) • 810 B
JavaScript
"use client";
import { useSafeLayoutEffect } from "../../utils/effect.js";
import { useCallback, useRef, useState } from "react";
//#region src/hooks/use-mounted/index.ts
function useMounted({ delay = 0, state = false } = {}) {
const mountedRef = useRef(false);
const [mounted, setMounted] = useState(false);
useSafeLayoutEffect(() => {
mountedRef.current = true;
let timeoutId = null;
if (state) if (delay > 0) timeoutId = setTimeout(() => setMounted(true), delay);
else setMounted(true);
return () => {
mountedRef.current = false;
if (state) setMounted(false);
if (timeoutId) clearTimeout(timeoutId);
};
}, [delay, state]);
if (state) return mounted;
else return useCallback(() => mountedRef.current, []);
}
//#endregion
export { useMounted };
//# sourceMappingURL=index.js.map