UNPKG

@anb98/react-hooks

Version:
30 lines (29 loc) 1.28 kB
import { AxiosRequestConfig } from 'axios'; import { UsePromiseOptions } from './usePromise'; export declare type RequestUseLazyFetch = Omit<AxiosRequestConfig, 'baseURL'>; export declare type UseLazyFetchProps<Response> = { url: string; request: Omit<RequestUseLazyFetch, 'url'>; } & Omit<UsePromiseOptions<RequestUseLazyFetch, Response>, 'params' | 'onUnmount'> & { onCancel: () => void; }; /** * This hook consumes an API when calling the handler function. * @param props Initial options * @returns [state, handler, resetState, cancelFetch] * @example * ``` const [state, handler, resetState, cancelFetch ] = useLazyFetch({ url: 'your-endpoint-url', initialData: {}, request: { headers: { example: 'test'} } onCancel: () => {}, onComplete: (data, err) => {}, onFail: (err) => {}, onSuccess: (data) => {}, }); ``` * @see https://www.npmjs.com/package/@anb98/react-hooks#useLazyFetch */ declare const useLazyFetch: <Response_1 = any>(props?: Partial<UseLazyFetchProps<Response_1>> | undefined) => readonly [import("./usePromise").State<Awaited<Response_1>>, (...promiseParams: [] | [request?: RequestUseLazyFetch | undefined]) => Promise<void>, () => void, () => void]; export default useLazyFetch;