feature/share-libs
ts 22 lines 454 Bytes
Raw
1 import { useState, useEffect } from 'react';
2
3 export function useCountDown() {
4 const [secondsLeft, setSecondsLeft] = useState(0);
5
6 useEffect(() => {
7 if (secondsLeft <= 0) return
8
9 const timerId = setTimeout(() => setSecondsLeft(secondsLeft - 1), 1000);
10 return () => clearTimeout(timerId);
11 }
12 , [secondsLeft]);
13
14 function startTimer(seconds: number) {
15 setSecondsLeft(seconds);
16 }
17
18 return {
19 secondsLeft,
20 startTimer,
21 };
22 }