UNPKG

767 BTypeScriptView Raw
1import React from 'react';
2export declare type AsyncSetState<TState> = (stateUpdate: React.SetStateAction<TState>) => Promise<TState>;
3/**
4 * A hook that mirrors `useState` in function and API, expect that setState
5 * calls return a promise that resolves after the state has been set (in an effect).
6 *
7 * This is _similar_ to the second callback in classy setState calls, but fires later.
8 *
9 * ```ts
10 * const [counter, setState] = useStateAsync(1);
11 *
12 * const handleIncrement = async () => {
13 * await setState(2);
14 * doWorkRequiringCurrentState()
15 * }
16 * ```
17 *
18 * @param initialState initialize with some state value same as `useState`
19 */
20export default function useStateAsync<TState>(initialState: TState | (() => TState)): [TState, AsyncSetState<TState>];