useEnsAvatar
Hook for fetching avatar for ENS name.
This is a wrapper around viem's getEnsAvatar
.
import { useEnsAvatar } from 'wagmi'
Usage
import { useEnsAvatar } from 'wagmi'
function App() {
const { data, isError, isLoading } = useEnsAvatar({
name: 'jxom.eth',
})
if (isLoading) return <div>Fetching avatar…</div>
if (isError) return <div>Error fetching avatar</div>
return <div>Avatar: {data}</div>
}
Return Value
{
data?: string
error?: Error
isIdle: boolean
isLoading: boolean
isFetching: boolean
isSuccess: boolean
isError: boolean
isFetched: boolean
isFetchedAfterMount: boolean
isRefetching: boolean
refetch: (options: {
throwOnError: boolean
cancelRefetch: boolean
}) => Promise<string>
status: 'idle' | 'error' | 'loading' | 'success'
}
Configuration
name (optional)
ENS name to fetch avatar for. If name
is not defined, hook will not run.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
})
}
chainId (optional)
Force a specific chain id for the request. The wagmi Client
's publicClient
must be set up as a chain-aware function for this to work correctly.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
chainId: 1,
})
}
cacheTime (optional)
Time (in ms) which the data should remain in the cache. Defaults to 0
.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
cacheTime: 2_000,
})
}
enabled (optional)
Set this to false
to disable this query from automatically running. Defaults to true
.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
enabled: false,
})
}
scopeKey (optional)
Scopes the cache to a given context. Hooks that have identical context will share the same cache.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
scopeKey: 'wagmi',
})
}
staleTime (optional)
Time (in ms) after data is considered stale. If set to Infinity
the data will never be considered stale. Defaults to 1000 * 60 * 60 * 24
(24 hours).
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
staleTime: 2_000,
})
}
suspense (optional)
Set this to true
to enable suspense mode.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
suspense: true,
})
}
onSuccess (optional)
Function to invoke when fetching new data is successful.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
onSuccess(data) {
console.log('Success', data)
},
})
}
onError (optional)
Function to invoke when an error is thrown while fetching new data.
import { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.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 { useEnsAvatar } from 'wagmi'
function App() {
const ensAvatar = useEnsAvatar({
name: 'jxom.eth',
onSettled(data, error) {
console.log('Settled', { data, error })
},
})
}