Skip to content

useClient

Hook for getting Viem Client instance.

Import

ts
import { useClient } from 'wagmi'
import { useClient } from 'wagmi'

Usage

tsx
import { useClient } from 'wagmi'

function App() {
  const client = useClient()
}
import { useClient } from 'wagmi'

function App() {
  const client = useClient()
}
ts
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

Parameters

ts
import { type UseClientParameters } from 'wagmi'
import { type UseClientParameters } from 'wagmi'

chainId

config['chains'][number]['id'] | undefined

ID of chain to use when getting Viem Client.

ts
import { useClient } from 'wagmi'
import { mainnet } from 'wagmi/chains' 
import { config } from './config'

function App() {
  const client = useClient({
    chainId: mainnet.id, 
  })
}
import { useClient } from 'wagmi'
import { mainnet } from 'wagmi/chains' 
import { config } from './config'

function App() {
  const client = useClient({
    chainId: mainnet.id, 
  })
}
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

config

Config | undefined

Config to use instead of retrieving from the from nearest WagmiProvider.

tsx
import { useClient } from 'wagmi'
import { config } from './config' 

function App() {
  const client = useClient({
    config, 
  })
}
import { useClient } from 'wagmi'
import { config } from './config' 

function App() {
  const client = useClient({
    config, 
  })
}
ts
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from 'wagmi'
import { mainnet, sepolia } from 'wagmi/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})

Return Type

ts
import { type UseClientReturnType } from 'wagmi'
import { type UseClientReturnType } from 'wagmi'

Client | undefined

Viem Client instance.

Action

Released under the MIT License.