@darwish/hooks-core
Version:
27 lines (26 loc) • 1 kB
TypeScript
/// <reference types="react" />
/**
* This hook is used to make requests to the server.
* It should be used in the components that need to make requests to the server.
* It should be used in the following way:
*
*/
export interface Options<TData, TParams> {
defaultData?: TData | null;
manual?: boolean;
defaultParams?: Partial<TParams>[];
refreshDeps?: React.DependencyList;
onSuccess?: (data: TData, params: TParams) => void;
onError?: (error: unknown) => void;
onBefore?: (params: TParams) => void;
onFinally?: (params: TParams, result: TData, error: Error) => void;
}
export default function useRequest<TData = null, TParams extends any[] = []>(requestFn: (...obj: any[]) => Promise<TData>, options?: Options<TData, TParams>): {
cancel: () => void;
data: TData | null;
run: (runParams: any) => void;
refresh: () => void;
mutate: (data: TData | ((prev: TData | null) => TData | null) | null) => void;
loading: boolean;
error: any;
};