UNPKG

795 BTypeScriptView Raw
1import { Dispatch, SetStateAction } from 'react';
2import { AsyncSetState } from './useStateAsync';
3declare type StateSetter<TState> = Dispatch<SetStateAction<TState>>;
4/**
5 * `useSafeState` takes the return value of a `useState` hook and wraps the
6 * setter to prevent updates onces the component has unmounted. Can used
7 * with `useMergeState` and `useStateAsync` as well
8 *
9 * @param state The return value of a useStateHook
10 *
11 * ```ts
12 * const [show, setShow] = useSafeState(useState(true));
13 * ```
14 */
15declare function useSafeState<TState>(state: [TState, AsyncSetState<TState>]): [TState, (stateUpdate: React.SetStateAction<TState>) => Promise<void>];
16declare function useSafeState<TState>(state: [TState, StateSetter<TState>]): [TState, StateSetter<TState>];
17export default useSafeState;