refactor: add request lock for zero deps for useAsync

pull/183/merge
moonrailgun 1 year ago
parent b1004434a1
commit fb59827ca5

@ -1,4 +1,4 @@
import { DependencyList, useEffect } from 'react'; import { DependencyList, useEffect, useRef } from 'react';
import type { FunctionReturningPromise } from '../types'; import type { FunctionReturningPromise } from '../types';
import { useAsyncFn } from './useAsyncFn'; import { useAsyncFn } from './useAsyncFn';
@ -11,8 +11,17 @@ export function useAsync<T extends FunctionReturningPromise>(
const [state, callback] = useAsyncFn(fn, deps, { const [state, callback] = useAsyncFn(fn, deps, {
loading: true, loading: true,
}); });
const lockRef = useRef(false);
useEffect(() => { useEffect(() => {
if (lockRef.current === true) {
return;
}
if (deps.length === 0) {
lockRef.current = true;
}
callback(); callback();
}, [callback]); }, [callback]);

Loading…
Cancel
Save