Hooks

useAsync

Publisher

cermuel

50 hooks published

More from this publisher

useAsync

Manage asynchronous execution state.

useAsync.tsx
import { useAsync } from "@cermuel/hooks/react";export function useAsyncExample() {  async function fetchUser() { return { name: 'Ada' }; }  const request = useAsync(fetchUser);  return <button type="button" onClick={() => void request.execute()}>Load</button>;}

Props

Param
Type
Default Value
fn
(...args: TArgs) => Promise<TResult>
options?
{ immediate?: boolean; args?: TArgs }
{}

Returns

{ status: AsyncStatus; data: TResult | null; error: Error | null; loading: boolean; execute: (...args: TArgs) => Promise<TResult>; reset: () => void; }