UNPKG

@anb98/react-hooks

Version:
28 lines (27 loc) 1.22 kB
import { UseLazyFetchProps, RequestUseLazyFetch } from './useLazyFetch'; declare type DependencyList = ReadonlyArray<any>; export declare type UseFetchProps<T> = Omit<UseLazyFetchProps<T>, 'url'> & { deps: DependencyList; }; export declare type RequestUseFetch = RequestUseLazyFetch; /** * This hook consumes API when component is mounted and also when calling the fetchHandler function. By default it request with GET method unless you change initial options. * @param {string} url URL to request * @param props Initial options * @example * ``` const [state, fetchHandler, resetState ] = useFetch('your-endpoint-url', { deps: [], initialData: {}, request: { headers: { example: 'test'} } onCancel: () => {}, onComplete: (data, err) => {}, onFail: (err) => {}, onSuccess: (data) => {}, }); * ``` * @see https://www.npmjs.com/package/@anb98/react-hooks#useFetch */ declare const useFetch: <T = any>(url: string, props?: Partial<UseFetchProps<T>> | undefined) => readonly [import("./usePromise").State<Awaited<T>>, (...promiseParams: [] | [request?: RequestUseLazyFetch | undefined]) => Promise<void>, () => void, () => void]; export default useFetch;