usePolling
A hook for performing interval-based API polling with control functions and error handling.
usePolling
The usePolling
hook provides a way to fetch data from an API endpoint at regular intervals. It includes comprehensive controls for starting, pausing, and stopping polling, along with error handling, retry limits, and state management.
Track | Asset | Price (USD) | 24h Change |
---|---|---|---|
B Bitcoin BTC | $56789.34 | +2.45% | |
E Ethereum ETH | $2834.56 | -1.2% | |
S Solana SOL | $123.78 | +5.67% | |
D Polkadot DOT | $34.56 | +0.89% | |
D Dogecoin DOGE | $0.1234 | -3.45% |
Toggle currencies to track specific assets
Data refreshes every 5 seconds
Installation
Install the usePolling hook using:
File Structure
Parameters
Prop | Type | Default |
---|---|---|
options | PollingOptions<T> | Required |
Return Value
Prop | Type | Default |
---|---|---|
data | T | null | - |
error | Error | null | - |
status | 'IDLE' | 'POLLING' | 'PAUSED' | 'ERROR' | 'STOPPED' | - |
lastUpdated | number | null | - |
errorCount | number | - |
isPolling | boolean | - |
isPaused | boolean | - |
isStopped | boolean | - |
isError | boolean | - |
startPolling | () => void | - |
pausePolling | () => void | - |
stopPolling | () => void | - |
resetErrors | () => void | - |
fetchNow | () => void | - |
Examples
Server Status Monitoring
A real-time dashboard for monitoring server health metrics with automatic polling.
Web Server 01
web | us-east
Fetching server status...
API Server 01
api | us-east
Fetching server status...
Database 01
database | us-east
Fetching server status...
Cache Server 01
cache | us-west
Fetching server status...
Worker 01
worker | eu-central
Fetching server status...
Monitoring 5 servers across 3 regions
Refreshes every 10 seconds
Real-time Stock Market Data
Auto-Saving Form
A form with automatic saving functionality that periodically sends updates to the server.
Use Cases
- Data Dashboards: Keep dashboards updated with the latest metrics and KPIs
- Financial Applications: Track stock prices, currency exchange rates, or cryptocurrency values
- Monitoring Systems: Poll server status, system health, or application metrics
- Chat Applications: Check for new messages when WebSockets aren’t available
- Form Auto-Save: Automatically save form data at regular intervals
- Content Updates: Refresh content feeds, news, or notifications
- IoT Applications: Poll devices for status updates or sensor data
- Sports/Gaming: Update live scores, match status, or game statistics
- Collaborative Editing: Poll for document changes from other users
Performance Considerations
- Battery Impact: Frequent polling can impact battery life on mobile devices
- Network Usage: Be mindful of data usage, especially on metered connections
- Server Load: Avoid polling too frequently to prevent overwhelming your server
- Variable Intervals: Consider using longer intervals when the application is not actively used
- Conditional Polling: Only poll when necessary (e.g., stop polling when a tab is not visible)
- Backoff Strategy: Implement exponential backoff for error retry attempts
Accessibility
When implementing polling in user interfaces:
- Use ARIA live regions to announce important updates to screen readers
- Provide visual indicators when data is being refreshed
- Allow users to pause automatic updates if needed
- Maintain focus position when content updates
Best Practices
- Debounce User Actions: Avoid triggering multiple fetches when users interact with controls
- Error Handling: Always handle errors gracefully with clear user feedback
- Loading States: Show loading indicators during long-running fetches
- Last Updated Time: Display when data was last updated for user awareness
- Cleanup: Always clean up timeouts when components unmount
- User Control: Give users control to pause, resume, or adjust polling frequency
- Batch Updates: For multiple polled resources, consider batching them to reduce requests