File tree Expand file tree Collapse file tree 1 file changed +6
-24
lines changed Expand file tree Collapse file tree 1 file changed +6
-24
lines changed Original file line number Diff line number Diff line change 1- import { useState , useEffect , useRef } from 'react' ;
1+ import { useState } from 'react' ;
22import { 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 ) ,
You can’t perform that action at this time.
0 commit comments