useTokenBalance
TheuseTokenBalance hook fetches token balances for a connected wallet with optional automatic refresh intervals.
Import
Signature
Parameters
UseTokenBalanceParams
Returns
UseTokenBalanceReturn
Features
- Automatic refresh - Set
refetchIntervalto keep balances up to date - Smart formatting - Balances are formatted with appropriate precision
- Native token support - Use
'native'for chain native tokens - Stale-while-revalidate - Previous balance shown during refresh
- Controlled fetching - Use
enabledto pause/resume
Basic Usage
Auto-Refresh Example
Keep balances updated automatically:Native Token Balance
Fetch the native token balance (ETH, MATIC, etc.):You can also use the zero address (
0x0000000000000000000000000000000000000000) instead of 'native'.Multiple Token Balances
Display balances for multiple tokens:Conditional Fetching
Control when balances are fetched:Integration with wagmi
Combine with wagmi for a complete wallet experience:Balance Formatting
The hook automatically formats balances for display:Error Handling
Best Practices
Performance and UX Tips:
- Choose refresh intervals wisely - Too frequent refreshes waste resources. 10-30 seconds is usually sufficient.
-
Use
enabledfor optimization - Disable fetching when the component is not visible or wallet is not connected. - Handle loading gracefully - Show previous balance while refreshing (stale-while-revalidate pattern).
-
Clear data on wallet change - The hook automatically clears data when
walletAddresschanges. -
Manual refetch clears interval - When you call
refetch(), the auto-refresh timer resets to prevent overlapping requests.
Next Steps
useQuote
Fetch bridge quotes
useTransactionStatus
Track transaction progress
