Size
0.93 kb
useCountdown
Countdown utility based on useInterval.
Installation
Usage
Get the remaining milliseconds:
Transform the remaining time in seconds:
Use a custom interval between each tick:
Prevent rerendering the component on every tick:
Synchronize all countdowns using the same interval:
Add more details on the current countdown state:
Background Tabs
Browsers throttle intervals in background tabs, which can leave the countdown stale.
When the tab becomes visible again, the countdown immediately resamples the remaining time and triggers onTick.
API Reference
UseCountdownOptions
The options of the `useCountdown` hook.
| Prop | Type | Default |
|---|---|---|
sync? | boolean | false |
trackState? | boolean | true |
onExpire? | (() => void) | - |
onTick? | ((value: T) => void) | - |
transform? | ((ms: number, to: To) => T) | - |
interval? | number | ((remainingMs: number) => number) | 1000 |
to | Date | string | null | undefined | false | - |