Skip to content

Commit ad47f04

Browse files
committed
fix: restructurar funcion de escucha para countdown
1 parent b7f7361 commit ad47f04

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

src/hooks/useCountdown.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import type { CountdownData } from "../types";
33

44
export function useCountdown(targetDate: number): CountdownData {
55
const [timeLeft, setTimeLeft] = useState<CountdownData>(() => {
6-
// Calcular estado inicial inmediatamente
76
const now = Date.now();
8-
9-
// 🔧 FIX: Validar targetDate
107
const validTargetDate =
118
targetDate && targetDate > now
129
? targetDate
@@ -48,8 +45,6 @@ export function useCountdown(targetDate: number): CountdownData {
4845
useEffect(() => {
4946
const calculateTimeLeft = () => {
5047
const now = Date.now();
51-
52-
// Validar targetDate también aquí
5348
const validTargetDate =
5449
targetDate && targetDate > now
5550
? targetDate
@@ -67,7 +62,6 @@ export function useCountdown(targetDate: number): CountdownData {
6762
isExpired: true,
6863
});
6964

70-
// Limpiar interval cuando expire
7165
if (intervalRef.current) {
7266
clearInterval(intervalRef.current);
7367
intervalRef.current = null;
@@ -93,11 +87,9 @@ export function useCountdown(targetDate: number): CountdownData {
9387
});
9488
};
9589

96-
// Calcular inmediatamente
97-
calculateTimeLeft();
98-
99-
// Solo crear interval si no existe y no ha expirado
100-
if (!intervalRef.current && !timeLeft.isExpired) {
90+
// Solo crear interval UNA VEZ
91+
if (!intervalRef.current) {
92+
calculateTimeLeft();
10193
intervalRef.current = setInterval(calculateTimeLeft, 1000);
10294
}
10395

@@ -107,7 +99,7 @@ export function useCountdown(targetDate: number): CountdownData {
10799
intervalRef.current = null;
108100
}
109101
};
110-
}, [targetDate, timeLeft.isExpired]);
102+
}, [targetDate]); // ✅ Solo re-ejecutar si targetDate cambia
111103

112104
return timeLeft;
113105
}

0 commit comments

Comments
 (0)