UNPKG

@modern-kit/react

Version:
37 lines (34 loc) 845 B
import { useState, useRef, useCallback } from 'react'; function useBlockMultipleAsyncCalls() { const [isLoading, setIsLoading] = useState(false); const [isError, setIsError] = useState(false); const isCalled = useRef(false); const blockMultipleAsyncCalls = useCallback( async (callback) => { if (isCalled.current) { return; } isCalled.current = true; setIsLoading(true); setIsError(false); try { const result = await callback(); return result; } catch (error) { setIsError(true); throw error; } finally { isCalled.current = false; setIsLoading(false); } }, [] ); return { isError, isLoading, blockMultipleAsyncCalls }; } export { useBlockMultipleAsyncCalls }; //# sourceMappingURL=index.mjs.map