useBalance
Hook for fetching balance information for Ethereum or ERC-20 tokens.
import { useBalance } from 'wagmi'
Usage
import { useBalance } from 'wagmi'
function App() {
const { data, isError, isLoading } = useBalance({
addressOrName: 'awkweb.eth',
})
if (isLoading) return <div>Fetching balance…</div>
if (isError) return <div>Error fetching balance</div>
return (
<div>
Balance: {data?.formatted} {data?.symbol}
</div>
)
}
Return Values
{
data?: {
decimals: number
formatted: string
symbol: string
value: BigNumber
}
error?: Error
isError: boolean
isFetched: boolean
isFetching: boolean
isIdle: boolean
isLoading: boolean
isRefetching: boolean
isStale: boolean
isSuccess: boolean
refetch: (options: {
throwOnError: boolean
cancelRefetch: boolean
}) => Promise<{
decimals: number
formatted: string
symbol: string
value: BigNumber
}>
status: 'idle' | 'error' | 'loading' | 'success'
}
Configuration
addressOrName (optional)
Address or ENS name to fetch balance for. If addressOrName
is not defined, hook will not run.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
})
}
formatUnits (optional)
Formats balance using ethers.js units. Defaults to ether
.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
formatUnits: 'gwei',
})
}
token (optional)
Address for ERC-20 token. If token
is provided, hook fetches token balance instead of Ethereum balance. For example, we can fetch awkweb.eth
's current $UNI balance.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
token: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
})
}
watch (optional)
Watches and refreshes data for new blocks.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
watch: true,
})
}
cacheTime (optional)
Time (in ms) which the data should remain in the cache. Defaults to 0
.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
cacheTime: 2_000,
})
}
enabled (optional)
Set this to false
to disable this query from automatically running. Defaults to true
.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
enabled: false,
})
}
staleTime (optional)
Time (in ms) after data is considered stale. If set to Infinity
the data will never be considered stale. Defaults to 0
.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
staleTime: 2_000,
})
}
suspense (optional)
Set this to true
to enable suspense mode.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
suspense: true,
})
}
onError (optional)
Function to invoke when an error is thrown while fetching new data.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
onError(error) {
console.log('Error', error)
},
})
}
onSettled (optional)
Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
onSettled(data, error) {
console.log('Settled', { data, error })
},
})
}
onSuccess (optional)
Function to invoke when fetching new data is successful.
import { useBalance } from 'wagmi'
function App() {
const balance = useBalance({
addressOrName: 'awkweb.eth',
onSuccess(data) {
console.log('Success', data)
},
})
}