Skip to content

Commit c659d7f

Browse files
committed
add isRunning state to useStopwatch
1 parent c4555ca commit c659d7f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/useStopwatch.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ export default function useStopwatch(settings) {
55
const { autoStart } = settings || {};
66

77
const [seconds, setSeconds] = useState(0);
8+
const [isRunning, setIsRunning] = useState(autoStart);
89
const intervalRef = useRef();
910

1011
function clearIntervalRef() {
1112
if (intervalRef.current) {
13+
setIsRunning(false);
1214
clearInterval(intervalRef.current);
1315
intervalRef.current = undefined;
1416
}
1517
}
1618

1719
function start() {
1820
if (!intervalRef.current) {
21+
setIsRunning(true);
1922
intervalRef.current = setInterval(() => setSeconds((prevSeconds) => (prevSeconds + 1)), 1000);
2023
}
2124
}
2225

2326
function pause() {
24-
if (intervalRef.current) {
25-
clearInterval(intervalRef.current);
26-
intervalRef.current = undefined;
27-
}
27+
clearIntervalRef();
2828
}
2929

3030
function reset() {
@@ -44,6 +44,6 @@ export default function useStopwatch(settings) {
4444
}, []);
4545

4646
return {
47-
...Time.getTimeFromSeconds(seconds), start, pause, reset,
47+
...Time.getTimeFromSeconds(seconds), start, pause, reset, isRunning,
4848
};
4949
}

0 commit comments

Comments
 (0)