Skip to content

useChainId

Hook for getting current chain ID.

Import

ts
import { useChainId } from 'wagmi'
import { useChainId } from 'wagmi'

Usage

tsx
import { useChainId } from 'wagmi'

function App() {
  const chainId = useChainId()
}
import { useChainId } from 'wagmi'

function App() {
  const chainId = useChainId()
}
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 UseChainIdParameters } from 'wagmi'
import { type UseChainIdParameters } from 'wagmi'

config

Config | undefined

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

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

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

function App() {
  const chainId = useChainId({
    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 UseChainIdReturnType } from 'wagmi'
import { type UseChainIdReturnType } from 'wagmi'

number

Current chain ID from config.state.chainId.

Action

Released under the MIT License.