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
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
callback | () => boolean or () => Promise<boolean> | Function to execute until it returns/resolves to true | - | No |
interval | number | Delay between retry attempts in milliseconds | 100 | Yes |
options | { maxRetries: number } | Configuration with maximum retry attempts | undefined | Yes |
Return Values
Name | Type | Description |
---|---|---|
hasResolved | boolean | true if the callback succeeded, false if still retrying or failed |