UNPKG

stream-chat-react

Version:

React components to create chat conversations or livestream style chat

20 lines (19 loc) 604 B
import { useEffect, useState } from 'react'; export const useTimer = ({ startFrom }) => { const [secondsLeft, setSecondsLeft] = useState(); useEffect(() => { let countdownTimeout; if (typeof secondsLeft === 'number' && secondsLeft > 0) { countdownTimeout = setTimeout(() => { setSecondsLeft(secondsLeft - 1); }, 1000); } return () => { clearTimeout(countdownTimeout); }; }, [secondsLeft]); useEffect(() => { setSecondsLeft(startFrom ?? 0); }, [startFrom]); return secondsLeft; };