Overview
TheuseCountdown hook provides a countdown timer that updates every second and triggers a callback when the timer expires. Itβs primarily used for quote expiration tracking.
Import
Signature
Parameters
Unix timestamp (in seconds) when the countdown should expire
Optional callback function to execute when the countdown reaches zero
Return Value
Number of seconds remaining until expiration
Formatted time string showing remaining seconds, or βExpiredβ when done
True when the countdown has reached zero
Usage Examples
Basic Countdown
Quote Expiration Timer
With Visual Progress
QuoteCountdown Component
The actualQuoteCountdown component in the application uses this hook:
components/QuoteCountdown.tsx
Implementation Details
Automatic Cleanup
The hook automatically cleans up the interval timer when:- The component unmounts
- The timestamp parameter changes
- The countdown reaches zero
Time Calculation
The countdown calculates remaining time by:- Converting Unix timestamp (seconds) to milliseconds
- Subtracting current time
- Flooring to nearest second
- Clamping to zero minimum
Best Practices
Use with quote expiration
Quotes from the OneBalance API have a 30-second validity period. Use this hook to track expiration and automatically refresh quotes.
Provide expiration callback
Always pass an
onExpire callback to handle what should happen when the timer runs out (e.g., refresh quote, show warning).Handle edge cases
Check
isExpired before allowing users to execute actions based on the countdown (e.g., prevent swap execution with expired quote).Related Hooks
- useQuotes - Uses countdown for quote expiration tracking
Related Components
- QuoteDetails - Displays quote information with countdown
- SwapForm - Uses countdown for quote validity