useCountdown
A hook that provides a numeric countdown with pause, play, and reset controls. Perfect for implementing countdowns between specific numbers, like game countdowns or timed interactions.
Parameters
min
:number
- The number to count down tomax
:number
- The number to start counting from
Returns
current
:string
- Current count value as stringisPaused
:boolean
- Whether the countdown is pausedisOver
:boolean
- Whether the countdown has reached min valuepause
:() => void
- Function to pause the countdownplay
:() => void
- Function to start/resume the countdownreset
:() => void
- Function to reset to max valuetogglePause
:() => void
- Function to toggle pause state
Usage
import { useCountdown } from '@rhinolabs/react-hooks';
function GameCountdown() {
const {
current,
isPaused,
isOver,
togglePause,
reset
} = useCountdown(0, 10);
return (
<div>
<h2>Count: {current}</h2>
<button onClick={togglePause}>
{isPaused ? 'Resume' : 'Pause'}
</button>
<button onClick={reset}>
Start Over
</button>
{isOver && (
<div>Countdown complete!</div>
)}
</div>
);
}
Notes
- Implements one-second interval countdown with boundary control
- Provides comprehensive playback controls (pause/play/reset)
- Maintains state with string-based current value
- Handles completion state and cleanup automatically
- Enforces min/max value constraints
Last updated on