UNPKG

use-async-reducer

Version:

Provides a reducer to simplify handling of async actions

50 lines (49 loc) 2.1 kB
export * from './actions'; export * from './constants'; export declare type Loadable<V = any> = { loading: boolean; error: Error | null; data: V; }; export declare const reducer: <T extends any>(state: Loadable<T>, action: import("typesafe-actions/dist/type-helpers").EmptyAction<import("./constants").ACTION_TYPES.INITIALIZE> | import("typesafe-actions/dist/type-helpers").EmptyAction<import("./constants").ACTION_TYPES.REQUEST> | import("typesafe-actions/dist/type-helpers").PayloadAction<import("./constants").ACTION_TYPES.SUCCESS, any> | import("typesafe-actions/dist/type-helpers").PayloadAction<import("./constants").ACTION_TYPES.FAILURE, Error> | import("typesafe-actions/dist/type-helpers").EmptyAction<import("./constants").ACTION_TYPES.COMPLETE>) => Loadable<any>; export interface AsyncReducerBoundActions<T = any> { /** * To be called at the beginning of a request, sets state to: ``` { data: null loading: true, error: null } ``` */ initialize(): void; /** * To be called at the beginning of async updates, sets `loading` to `true` */ request(): void; /** * To be called with the data to be saved into the state * @param payload Result of the async call */ success(payload: T): void; /** * To be called when the async call fails * @param error */ failure(error: Error): void; /** * Can be called when a call fails/complete and the result is being discarded */ complete(): void; } /** * Provides a reducer and actions to manage states of async operations * @param initialValue Will set the `data` attribute of the first returned value * @returns An array of two values * 0. the state of the async operation with the shape * 1. object of bound action methods * * @see [https://github.com/azmenak/use-async-reducer/blob/master/README.md](https://github.com/azmenak/use-async-reducer/blob/master/README.md) */ export default function useAsyncReducer<V extends any>(initialValue?: V): [Loadable<V>, AsyncReducerBoundActions<V>];