Skip to content

Commit ed2f0ae

Browse files
author
Hoa Phan
committed
fix: Fix Countdown resets on every setState
1 parent c859b75 commit ed2f0ae

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/CountDown/index.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
/* eslint-disable no-bitwise */
23
import React, {
34
useCallback,
@@ -61,7 +62,7 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
6162
onPause(currentSeconds.current);
6263
}
6364
clear();
64-
}, [onPause]);
65+
}, []);
6566

6667
const resume = () => {
6768
if (!interval.current) {
@@ -79,15 +80,13 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
7980
};
8081

8182
useEffect(() => {
83+
init();
84+
8285
return () => {
8386
pause();
8487
init();
8588
};
86-
}, [init, pause]);
87-
88-
useEffect(() => {
89-
init();
90-
}, [init, initialSeconds]);
89+
}, []);
9190

9291
const timer = useCallback(() => {
9392
interval.current = BackgroundTimer.setInterval(() => {
@@ -109,21 +108,21 @@ const CountdownComponent = React.forwardRef<any, CountDownProps>(
109108
}
110109
setKey(Math.random());
111110
}, 1000);
112-
}, [onEnd, onTimes]);
111+
}, []);
113112

114113
const start = useCallback(() => {
115114
init();
116115

117116
if (!interval.current) {
118117
timer();
119118
}
120-
}, [init, timer]);
119+
}, []);
121120

122121
useEffect(() => {
123122
if (autoStart) {
124123
start();
125124
}
126-
}, [autoStart, initialSeconds, start]);
125+
}, [autoStart]);
127126

128127
const clear = () => {
129128
if (interval.current) {

src/Timer/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
/* eslint-disable no-bitwise */
23
import React, {
34
useCallback,
@@ -62,7 +63,7 @@ const TimerComponent = React.forwardRef<any, TimerProps>((props, ref) => {
6263
}
6364
setKey(Math.random());
6465
}, 1000);
65-
}, [onTimes]);
66+
}, []);
6667

6768
const initTime = useCallback((iSeconds: number) => {
6869
if (iSeconds >= 3600) {
@@ -85,27 +86,29 @@ const TimerComponent = React.forwardRef<any, TimerProps>((props, ref) => {
8586
hours.current = 0;
8687
minute.current = 0;
8788
seconds.current = 0;
89+
8890
if (initialSeconds > 0) {
8991
initTime(initialSeconds);
9092
}
93+
9194
clear();
9295
setKey(Math.random());
93-
}, [initTime, initialSeconds]);
96+
}, [initialSeconds]);
9497

9598
const start = useCallback(() => {
9699
init();
97100

98101
if (!interval.current) {
99102
timer();
100103
}
101-
}, [init, timer]);
104+
}, []);
102105

103106
const pause = useCallback(() => {
104107
clear();
105108
if (onPause) {
106109
onPause(currentSeconds.current);
107110
}
108-
}, [onPause]);
111+
}, []);
109112

110113
const resume = () => {
111114
if (!interval.current) {
@@ -136,7 +139,7 @@ const TimerComponent = React.forwardRef<any, TimerProps>((props, ref) => {
136139
init();
137140
setKey(Math.random());
138141
};
139-
}, [init, pause]);
142+
}, []);
140143

141144
useEffect(() => {
142145
if (initialSeconds > 0) {

0 commit comments

Comments
 (0)