use-async-call
Version:
Provides a hook to provide state around an async function call
104 lines (103 loc) • 3.87 kB
TypeScript
import { Loadable, AsyncReducerBoundActions } from 'use-async-reducer';
export { Loadable } from 'use-async-reducer';
export interface UseAsyncCallOptions<T> {
/**
* Initial value used for `data` of state
*/
initialValue?: T;
/**
* When true, will not call `actions.initalize` when `asyncCreator` updates
* This keeps the data in the store between updates, useful when the identity
* of the data does not belong to the inputs, example would be a search
* component that uses "search text" as an input
*/
dontReinitialize?: boolean;
/**
* Useful when you need to register a hook, but are still waiting for other
* async data to contrust a request.
*
* When any values are falsey, `useAsyncCall` will not make any calls to the
* async creator and will return a `Loadable` response with
* null `data` and `loading=false`
*/
waitFor?: ReadonlyArray<any>;
/**
* Callback called after call is successful
* @param data Data returned from async caller
*/
onSuccess?(data?: T): void;
/**
* Callback called after async call throws
* @param error Error thrown by async caller
*/
onFailure?(error?: Error): void;
/**
* Callback always called after async call completes
*/
onComplete?(): void;
}
export interface UseAsyncCallUpdateOptions<T> {
/**
* Should thrown errors be re-thrown in the resulting promise from `update`;
* useful when using in conjuction with form libraries that expect errors when
* submitting form values
*/
throwError?: boolean;
/**
* If the caller throws, sets `state.error` to the error and `state.data` to
* `null`
*/
saveError?: boolean;
/**
* Callback called after call is successful
* @param data Data returned from async caller
*/
onSuccess?(data?: T): void;
/**
* Callback called after async call throws
* @param error Error thrown by async caller
*/
onFailure?(error?: Error): void;
/**
* Callback always called after async call completes
*/
onComplete?(): void;
}
export declare type UseAsyncCallUpdater<T> = (asyncUpdater: Promise<T> | (() => Promise<T>), updateOptions?: UseAsyncCallUpdateOptions<T>) => Promise<T | undefined>;
export declare type UseAsyncCellReturnType<T> = [Loadable<T>, UseAsyncCallReturnTypeOptions<T>];
export interface UseAsyncCallReturnTypeOptions<T> {
/**
* Method used to update the state though async calls, accepts a promise or
* a method which returns a promise, the state data will be updated to the
* result of the promise
*/
update: UseAsyncCallUpdater<T>;
/**
* When called, the method passed to `useAsyncCall` will be invoked
*/
refresh(): void;
/**
* Provides access to the reducer actions
*/
actions: AsyncReducerBoundActions<T>;
}
/**
* Returns a Symbol which can be used as a dep
*/
export declare function useRefreshSymbol(): [Symbol, () => void];
/**
* Returns previous value, or null if first render pass
* @param value updating value
*/
export declare function usePrevious<T extends any>(value: T): T | null;
/**
* Provides an abstraction to manage async state
* @param asyncCreator Async method to call, create this method with `useCallback` if it uses state from the component
* @param options
* @returns An array of two values
* 0. State of the async operation, an object with keys `data`, `loading`, `error`
* 1. An object of dispatch method to update the state
*
* @see [https://github.com/azmenak/use-async-call/blob/master/README.md](https://github.com/azmenak/use-async-call/blob/master/README.md)
*/
export default function useAsyncCall<T extends any>(asyncCreator: () => Promise<T>, options?: UseAsyncCallOptions<T>): UseAsyncCellReturnType<T>;