优化
This commit is contained in:
29
client/shared/hooks/useAsync.ts
Normal file
29
client/shared/hooks/useAsync.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { DependencyList, useEffect, useRef } from 'react';
|
||||
import type { FunctionReturningPromise } from '../types';
|
||||
import { useAsyncFn } from './useAsyncFn';
|
||||
|
||||
// Reference: https://github.com/streamich/react-use/blob/master/src/useAsync.ts
|
||||
|
||||
export function useAsync<T extends FunctionReturningPromise>(
|
||||
fn: T,
|
||||
deps: DependencyList = []
|
||||
) {
|
||||
const [state, callback] = useAsyncFn(fn, deps, {
|
||||
loading: true,
|
||||
});
|
||||
const lockRef = useRef(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (lockRef.current === true) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (deps.length === 0) {
|
||||
lockRef.current = true;
|
||||
}
|
||||
|
||||
callback();
|
||||
}, [callback]);
|
||||
|
||||
return state;
|
||||
}
|
||||
Reference in New Issue
Block a user