Prompt
Build a React hook called useCountdown that runs a countdown from a starting number of seconds down to zero.
const { isRunning, start, stop, seconds } = useCountdown(totalTime);What the hook returns:
seconds: the current number of seconds left. Starts attotalTime.isRunning:truewhile the countdown is active,falseotherwise.start: a function that begins the countdown.stop: a function that ends the countdown and putssecondsback tototalTime.
How the countdown behaves:
- At the start, the countdown is idle:
secondsistotalTimeandisRunningisfalse. - After calling
start(),secondsdrops by 1 every second until it reaches zero. - If
stop()is called at any point, the countdown ends right away andsecondsreturns tototalTime. - When the countdown runs all the way to zero on its own, the hook behaves as if
stop()was called:isRunningbecomesfalseandsecondsis reset tototalTime.
What the demo page should show:
- Two buttons: one labeled Start, one labeled Stop.
- While the countdown is running, display the current value of
seconds. - While the countdown is idle, display the text
No Timer Running.