You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/client/shared/hooks/useAsyncRefresh.ts

26 lines
533 B
TypeScript

import { DependencyList, useCallback, useEffect } from 'react';
import type { FunctionReturningPromise } from '../types';
import { useAsyncFn } from './useAsyncFn';
export function useAsyncRefresh<T extends FunctionReturningPromise>(
fn: T,
deps: DependencyList = []
) {
const [state, callback] = useAsyncFn(fn, deps, {
loading: true,
});
useEffect(() => {
callback();
}, [callback]);
const refresh = useCallback(() => {
return callback();
}, [callback]);
return {
...state,
refresh,
};
}