1.9 KiB
1.9 KiB
title, description
| title | description |
|---|---|
| usePublicClient | Hook for getting Viem `PublicClient` instance. |
usePublicClient
Hook for getting Viem PublicClient instance.
Import
import { usePublicClient } from 'wagmi'
Usage
::: code-group
import { usePublicClient } from 'wagmi'
function App() {
const client = usePublicClient()
}
<<< @/snippets/react/config.ts[config.ts] :::
::: warning
If you want to optimize bundle size, you should use useClient along with Viem's tree-shakable actions instead. Since Public Client has all public actions attached directly to it.
:::
Parameters
import { type UsePublicClientParameters } from 'wagmi'
chainId
config['chains'][number]['id'] | undefined
ID of chain to use when getting Viem Public Client.
::: code-group
import { usePublicClient } from 'wagmi'
import { mainnet } from 'wagmi/chains' // [!code focus]
import { config } from './config'
function App() {
const client = usePublicClient({
chainId: mainnet.id, // [!code focus]
})
}
<<< @/snippets/core/config.ts[config.ts] :::
config
Config | undefined
Config to use instead of retrieving from the nearest WagmiProvider.
::: code-group
import { usePublicClient } from 'wagmi'
import { config } from './config' // [!code focus]
function App() {
const client = usePublicClient({
config, // [!code focus]
})
}
<<< @/snippets/react/config.ts[config.ts] :::
Return Type
import { type UsePublicClientReturnType } from 'wagmi'
PublicClient | undefined
Viem PublicClient instance.