Skip to content

useAsync ​

Hook that provides the state of an async callback

utilities
test coverage
Last changed: 7 days ago

Installation ​

Library
CLI
Manual
typescript
import { useAsync } from '@siberiacancode/reactuse';

Usage ​

typescript
const { data, isLoading, isError, error } = useAsync(() => fetch('url'), [deps]);

Demo ​

Api ​

Parameters

NameTypeDefaultNote
callback() => Promise<Data>-The async callback
depsDependencyList-The dependencies of the callback

Returns

UseAsyncReturn<Data>

Type declaration ​

typescript
import type { DependencyList } from 'react';

export interface UseAsyncReturn<Data> {
  /* The state of the query */
  data?: Data;
  /* The error of the query */
  error?: Error;
  /* The error state of the query  */
  isError: boolean;
  /* The loading state of the query */
  isLoading: boolean;
}

Source ​

Source • Demo

Contributors ​

B
babin
babin
D
debabin
debabin

Released under the MIT License.