Skip to content
Docs
/
Hooks
/
useEnsName

useEnsName

Hook for fetching the ENS name for address.

import { useEnsName } from 'wagmi'

Usage

import { useEnsName } from 'wagmi'

function App() {
  const { data, isError, isLoading } = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
  })

  if (isLoading) return <div>Fetching name…</div>
  if (isError) return <div>Error fetching name</div>
  return <div>Name: {data}</div>
}

Return Value

{
  data?: string
  error?: Error
  isIdle: boolean
  isLoading: boolean
  isFetching: boolean
  isSuccess: boolean
  isError: boolean
  isFetched: boolean
  isRefetching: boolean
  refetch: (options: {
    throwOnError: boolean
    cancelRefetch: boolean
  }) => Promise<string>
  status: 'idle' | 'error' | 'loading' | 'success'
}

Configuration

address (optional)

Address to fetch ENS name for. If address is not defined, hook will not run.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac',
  })
}

chainId (optional)

Force a specific chain id for the request. The wagmi Client's ethers provider must be set up as a chain-aware function for this to work correctly.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    chainId: 1,
  })
}

cacheTime (optional)

Time (in ms) which the data should remain in the cache. Defaults to 0.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    cacheTime: 2_000,
  })
}

enabled (optional)

Set this to false to disable this query from automatically running. Defaults to true.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    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 1000 * 60 * 60 * 24 (24 hours).

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    staleTime: 2_000,
  })
}

suspense (optional)

Set this to true to enable suspense mode.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    suspense: true,
  })
}

onSuccess (optional)

Function to invoke when fetching new data is successful.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    onSuccess(data) {
      console.log('Success', data)
    },
  })
}

onError (optional)

Function to invoke when an error is thrown while fetching new data.

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    onError(error) {
      console.log('Error', error)
    },
  })
}

onSettled (optional)

Function to invoke when fetching is settled (either successfully fetched, or an error has thrown).

import { useEnsName } from 'wagmi'

function App() {
  const ensName = useEnsName({
    address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    onSettled(data, error) {
      console.log('Settled', { data, error })
    },
  })
}