Skip to content

useChain

The useChain hook provides access to the currently selected chain information including chain details and chain ID.

Import

tsx
import { useChain } from '@luno-kit/react'
import type { Chain } from '@luno-kit/react'

Usage

Basic Chain Information

tsx
import { useChain } from '@luno-kit/react'

function ChainInfo() {
  const { chain, chainId } = useChain()
  
  if (!chain) {
    return <div>No chain selected</div>
  }
  
  return (
    <div>
      <h3>Current Chain</h3>
      <p>Name: {chain.name}</p>
      <p>Chain ID: {chainId}</p>
      <p>Symbol: {chain.nativeCurrency.symbol}</p>
      <p>Decimals: {chain.nativeCurrency.decimals}</p>
    </div>
  )
}
ts
import { createConfig, kusama, polkadot, polkadotjsConnector } from '@luno-kit/react'

const config = createConfig({
  appName: 'luno with-vite example',
  chains: [polkadot, kusama],
  connectors: [polkadotjsConnector()],
  autoConnect: true,
});

Return Value

The hook returns an object with the following properties:

PropertyTypeDescription
chainChain | undefinedCurrent chain information
chainIdstring | undefinedCurrent chain ID (genesis hash)

Chain Object

The chain object contains:

PropertyTypeDescription
genesisHashstringChain genesis hash
namestringChain display name
nativeCurrency{ name: string; symbol: string; decimals: number }Native token information
rpcUrls{ webSocket: readonly string[]; http?: readonly string[] }RPC endpoint URLs
ss58FormatnumberSS58 address format
blockExplorers{ default?: { name: string; url: string }; [key: string]: { name: string; url: string } | undefined } | undefinedBlock explorer information
testnetbooleanWhether this is a testnet
chainIconUrlstringURL to chain icon

Released under the MIT License.