Overview
TheuseProtocolData hook provides a React-friendly interface to fetch and auto-refresh DeFi protocol data from DefiLlama. It handles loading states, error management, and automatic updates every 5 minutes.
Location: src/hooks/useProtocolData.js
Import
Signature
Return Values
Array of protocol data objects with live TVL and APY
true during initial data fetch, false once completeError message if fetch failed, otherwise
nullTimestamp of last successful data refresh,
null before first loadUsage Examples
Features
Auto-Refresh
Automatically refetches data every 5 minutes to keep protocol data current
Static Fallback
Starts with static
PROTOCOL_META data for instant UI renderingError Handling
Captures and exposes fetch errors without breaking the UI
Cleanup Safe
Cancels pending updates when component unmounts to prevent memory leaks
Behavior
Initial Load
- Hook starts with static protocol metadata from
PROTOCOL_META loadingistrue- Immediately fetches live data from DefiLlama
- Updates
protocolswith enriched data - Sets
loadingtofalseandlastUpdatedto current time
Auto-Refresh
- Sets up 5-minute interval timer on mount
- Silently refetches data in background
- Updates
protocolsandlastUpdatedon success - Does not show loading spinner during refresh (UX optimization)
Cleanup
Implementation Details
Source Code
Performance
Initial Render: Instant with static data
First Load: ~1-2s for parallel API calls
Refresh Interval: 5 minutes (300,000ms)
Memory: Cleaned up on unmount
First Load: ~1-2s for parallel API calls
Refresh Interval: 5 minutes (300,000ms)
Memory: Cleaned up on unmount
Comparison with Service
| Feature | useProtocolData Hook | fetchAllProtocolData Service |
|---|---|---|
| Usage | React components | Any JavaScript context |
| State Management | Built-in with useState | Manual |
| Auto-Refresh | Yes (5 min interval) | No |
| Error Handling | Exposed as state | Try/catch required |
| Loading State | Built-in | Manual |
| Cleanup | Automatic | N/A |
Related
DefiLlama Service
Underlying service that fetches protocol data
useWalletProtocols Hook
Detect wallet positions in protocols