UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

41 lines (37 loc) 885 B
"use client"; import { useUnmountEffect } from "../../utils/effect.js"; import { useBoolean } from "../use-boolean/index.js"; import { useCallback, useMemo, useRef } from "react"; //#region src/hooks/use-processing/index.ts /** * `useProcessing` is a custom hook for handling processing states. * * @see https://yamada-ui.com/docs/hooks/use-processing */ const useProcessing = (init) => { const [loading, { off, on }] = useBoolean(init); const countRef = useRef(0); const start = useCallback(() => { countRef.current += 1; on(); }, [on]); const finish = useCallback(() => { countRef.current -= 1; if (countRef.current <= 0) off(); }, [off]); useUnmountEffect(() => { countRef.current = 0; }); return useMemo(() => ({ finish, loading, start }), [ finish, loading, start ]); }; //#endregion export { useProcessing }; //# sourceMappingURL=index.js.map