Skip to main content

Overview

The useProtocolData 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 complete
string | null
required
Error message if fetch failed, otherwise null
Date | null
required
Timestamp of last successful data refresh, null before first load

Usage 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 rendering

Error 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

  1. Hook starts with static protocol metadata from PROTOCOL_META
  2. loading is true
  3. Immediately fetches live data from DefiLlama
  4. Updates protocols with enriched data
  5. Sets loading to false and lastUpdated to current time

Auto-Refresh

  • Sets up 5-minute interval timer on mount
  • Silently refetches data in background
  • Updates protocols and lastUpdated on success
  • Does not show loading spinner during refresh (UX optimization)

Cleanup

Prevents state updates if component unmounts during fetch.

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

Comparison with Service

DefiLlama Service

Underlying service that fetches protocol data

useWalletProtocols Hook

Detect wallet positions in protocols