Docs
useContinuousRetry

Automates retries of a callback function until it succeeds with useContinuousRetry.

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-continuous-retry

Usage

"use client"
 
import * as React from "react"
 
import { useContinuousRetry } from "@/hooks/use-continuous-retry"
 
export function Component() {
  const [connectionAttempts, setAttempts] = React.useState(0)
 
  const isConnected = useContinuousRetry(
    async () => {
      const success = await checkConnection()
      setAttempts((prev) => prev + 1)
      return success
    },
    1000,
    { maxRetries: 5 }
  )
 
  return (
    <div>
      <p>Connection status: {isConnected ? "Connected" : "Retrying..."}</p>
      <p>Attempts made: {connectionAttempts}</p>
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
callback() => boolean or () => Promise<boolean>Function to execute until it returns/resolves to true-No
intervalnumberDelay between retry attempts in milliseconds100Yes
options{ maxRetries: number }Configuration with maximum retry attemptsundefinedYes

Return Values

NameTypeDescription
hasResolvedbooleantrue if the callback succeeded, false if still retrying or failed