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<ProtocolData>
required
Array of protocol data objects with live TVL and APY
boolean
required
true during initial data fetch, false once completestring | null
required
Error message if fetch failed, otherwise
nullDate | null
required
Timestamp 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
Related
DefiLlama Service
Underlying service that fetches protocol data
useWalletProtocols Hook
Detect wallet positions in protocols