useRetryFn

This Hook is available since v1.4.0.

A React Hook that helps to run a function with retry mechanism.

Demo

Source

Usage

See API for more details.

Source

Click links below to view source on GitHub.

API

const fnWithRetry = useRetryFn(fn, options)

Fn

A function that will be called with retry mechanism.

Options

export interface UseRetryFnOptions<E = unknown> {
  /**
   * Number of retries.
   *
   * @defaultValue 3
   */
  count?: number
  /**
   * Retry interval. ms
   *
   * @defaultValue defaultRetryInterval
   */
  interval?: number | ((currentCount: number) => number)
  /**
   * Error callback.
   *
   * @defaultValue undefined
   */
  onError?: (error: E | undefined) => void
  /**
   * Error retry callback.
   *
   * @defaultValue undefined
   */
  onErrorRetry?: (error: E | undefined, state: UseRetryFnRetryState) => void
  /**
   * All retries failed callback.
   *
   * @defaultValue undefined
   */
  onRetryFailed?: (error: E | undefined, state: UseRetryFnRetryState) => void
}

Returns

A function with retry mechanism, with the same signature as the input function, with an additional cancel method.

export function useRetryFn<T extends AnyFunc, E = any>(
  fn: T,
  options: UseRetryFnOptions<E> = {},
): T & { cancel: () => void }