useStateAsync

    API

    import useStateAsync from '@restart/hooks/useStateAsync'
    • useStateAsync<TState>(initialState: TState | () => TState) => [TState,AsyncSetState<TState>]

      A hook that mirrors useState in function and API, expect that setState calls return a promise that resolves after the state has been set (in an effect). This is similar to the second callback in classy setState calls, but fires later.

      const [counter, setState] = useStateAsync(1);
      const handleIncrement = async () => {
      await setState(2);
      doWorkRequiringCurrentState()
      }

      Parameters

      • initialStateTState | () => TState

        initialize with some state value same as useState

      Return Value [TState,AsyncSetState<TState>]