• React
  • Hooks
  • useWalletClient

useWalletClient

Hook for accessing viem's Wallet Client for a connected account.

import { useWalletClient } from 'wagmi'

Usage

The following examples use the ENS Registry contract.

import { useWalletClient } from 'wagmi'
import { getContract } from 'wagmi/actions'
 
function App() {
  const { data: walletClient, isError, isLoading } = useWalletClient()
 
  const contract = getContract({
    address: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
    abi: ensRegistryABI,
    walletClient,
  })
}

Return Value

{
  data?: WalletClient
  error?: Error
  isIdle: boolean
  isLoading: boolean
  isFetching: boolean
  isSuccess: boolean
  isError: boolean
  isFetched: boolean
  isFetchedAfterMount: boolean
  isRefetching: boolean
  status: 'idle' | 'error' | 'loading' | 'success'
}

Configuration

chainId (optional)

Chain ID to use for Wallet Client.

import { useContract, useWalletClient } from 'wagmi'
import { optimism } from 'wagmi/chains'
 
function App() {
  const { data: walletClient } = useWalletClient({
    chainId: optimism.id,
  })
}

onError (optional)

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

import { useContract, useWalletClient } from 'wagmi'
 
function App() {
  const { data: walletClient } = useWalletClient({
    onError(error) {
      console.log('Error', error)
    },
  })
}

onSettled (optional)

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

import { useContract, useWalletClient } from 'wagmi'
 
function App() {
  const { data: walletClient } = useWalletClient({
    onSettled(data, error) {
      console.log('Settled', data, error)
    },
  })
}

onSuccess (optional)

Function to invoke when fetching new data is successful.

import { useContract, useWalletClient } from 'wagmi'
 
function App() {
  const { data: walletClient } = useWalletClient({
    onSuccess(data) {
      console.log('Success', data)
    },
  })
}