Skip to content

useChainId

Composable for getting current chain ID.

Import

ts
import { useChainId } from '@wagmi/vue'

Usage

vue
<script setup lang="ts">
import { useChainId } from '@wagmi/vue'

const chainId = useChainId()
</script>
ts
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'

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

Parameters

ts
import { type UseChainIdParameters } from '@wagmi/vue'

config

Config | undefined

Config to use instead of retrieving from the WagmiPlugin.

vue
<script setup lang="ts">
import { useChainId } from '@wagmi/vue'
import { config } from './config'

const chainId = useChainId({
  config, 
})
</script>
ts
import { http, createConfig } from '@wagmi/vue'
import { mainnet, sepolia } from '@wagmi/vue/chains'

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

Return Type

ts
import { type UseChainIdReturnType } from '@wagmi/vue'

number

Current chain ID from config.state.chainId.

INFO

Only returns chain IDs for chains configured via createConfig's chains parameter.

If the active connection chainId is not from a chain included in your Wagmi Config, useChainId will return the last configured chain ID.

Action

Released under the MIT License.