Skip to content

Commit c7a1cdd

Browse files
committed
handle useTime using useInterval
1 parent dffecde commit c7a1cdd

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

src/useTime.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
1-
import { useState, useEffect, useRef } from 'react';
1+
import { useState } from 'react';
22
import { Time } from './utils';
3+
import { useInterval } from './hooks';
34

4-
export default function useTime(settings) {
5-
const { format } = settings || {};
6-
5+
export default function useTime({ format }) {
76
const [seconds, setSeconds] = useState(Time.getSecondsFromTimeNow());
8-
const intervalRef = useRef();
9-
10-
function clearIntervalRef() {
11-
if (intervalRef.current) {
12-
clearInterval(intervalRef.current);
13-
intervalRef.current = undefined;
14-
}
15-
}
16-
17-
function start() {
18-
if (!intervalRef.current) {
19-
intervalRef.current = setInterval(() => setSeconds(Time.getSecondsFromTimeNow()), 1000);
20-
}
21-
}
22-
23-
// didMount effect
24-
useEffect(() => {
25-
start();
26-
return clearIntervalRef;
27-
}, []);
287

8+
useInterval(() => {
9+
setSeconds(Time.getSecondsFromTimeNow());
10+
}, 1000);
2911

3012
return {
3113
...Time.getFormattedTimeFromSeconds(seconds, format),

0 commit comments

Comments
 (0)