@@ -99,6 +99,8 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
99
99
setTimeUntilNextRetry ( null ) ;
100
100
setCurrentDelay ( null ) ;
101
101
clearTimers ( ) ;
102
+ // Increment attempt count when starting the retry
103
+ setAttemptCount ( prev => prev + 1 ) ;
102
104
103
105
try {
104
106
await onRetryEvent ( ) ;
@@ -107,21 +109,20 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
107
109
setIsRetrying ( false ) ;
108
110
setIsManualRetry ( false ) ;
109
111
} catch ( error ) {
110
- // If retry fails, schedule next attempt (if not manual and under max attempts)
111
- setAttemptCount ( ( prev ) => prev + 1 ) ;
112
+ // If retry fails, just update state (attemptCount already incremented)
112
113
setIsRetrying ( false ) ;
113
114
setIsManualRetry ( false ) ;
114
115
}
115
116
} , [ onRetryEvent , clearTimers ] ) ;
116
117
117
118
const scheduleNextRetry = useCallback (
118
119
( attempt : number ) => {
119
- if ( attempt >= maxAttempts ) {
120
+ if ( attempt > maxAttempts ) {
120
121
return ;
121
122
}
122
123
123
- // Calculate delay based on attempt - 2 (so second attempt gets initialDelay)
124
- const delay = calculateDelay ( Math . max ( 0 , attempt - 2 ) ) ;
124
+ // Calculate delay based on attempt - 1 (so first retry gets initialDelay)
125
+ const delay = calculateDelay ( Math . max ( 0 , attempt - 1 ) ) ;
125
126
setCurrentDelay ( delay ) ;
126
127
setTimeUntilNextRetry ( delay ) ;
127
128
startTimeRef . current = Date . now ( ) ;
@@ -155,8 +156,8 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
155
156
if (
156
157
! isRetrying &&
157
158
! isManualRetry &&
158
- attemptCount > 1 &&
159
- attemptCount <= maxAttempts
159
+ attemptCount > 0 &&
160
+ attemptCount < maxAttempts
160
161
) {
161
162
scheduleNextRetry ( attemptCount ) ;
162
163
}
@@ -172,7 +173,6 @@ export function useRetry(options: UseRetryOptions): UseRetryReturn {
172
173
173
174
const startRetrying = useCallback ( ( ) => {
174
175
// Immediately perform the first retry attempt
175
- setAttemptCount ( 1 ) ;
176
176
performRetry ( ) ;
177
177
} , [ performRetry ] ) ;
178
178
0 commit comments