Skip to content

useSwitchChain

The useSwitchChain hook provides functionality to switch between different chains and access chain switching state.

Import

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

Usage

Basic Chain Switching

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

function ChainSwitcher() {
  const { switchChain, chains, currentChain, isPending } = useSwitchChain()
  
  const handleSwitchChain = (chainId: string) => {
    switchChain({ chainId })
  }
  
  return (
    <div>
      <h3>Current Chain: {currentChain?.name}</h3>
      <div>
        {chains.map((chain) => (
          <button
            key={chain.genesisHash}
            onClick={() => handleSwitchChain(chain.genesisHash)}
            disabled={chain.genesisHash === currentChain?.genesisHash}
          >
            {chain.name} ({chain.nativeCurrency.symbol})
          </button>
        ))}
      </div>
    </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,
});

Async Chain Switching

tsx
function AsyncChainSwitcher() {
  const { switchChainAsync, isPending, error } = useSwitchChain()
  
  const handleSwitchChain = async (chainId: string) => {
    try {
      await switchChainAsync({ chainId })
      console.log('Chain switched successfully!')
    } catch (err) {
      console.error('Chain switching failed:', err)
    }
  }
  
  return (
    <div>
      <button onClick={() => handleSwitchChain('0x91b171bb158e2d8b7')} disabled={isPending}>
        {isPending ? 'Switching...' : 'Switch to Polkadot'}
      </button>
      {error && <p>Error: {error.message}</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,
});

Parameters

The hook accepts an optional configuration object:

ParameterTypeRequiredDescription
onSuccess() => voidNoCallback when chain switching succeeds
onError(error: Error) => voidNoCallback when chain switching fails
onSettled() => voidNoCallback when chain switching attempt completes

Return Value

The hook returns an object with the following properties:

Methods

PropertyTypeDescription
switchChain(variables: { chainId: string }, options?: options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => voidSwitch to a different chain
switchChainAsync(variables: { chainId: string }, options?: options?: { onSuccess?: () => void; onError?: (error: Error) => void; onSettled?: () => void }) => Promise<void>Async version of switchChain
reset() => voidReset the chain switching state

Data & State

PropertyTypeDescription
chainsChain[]Array of all available chains
currentChainChain | undefinedCurrently selected chain
currentChainIdstring | undefinedCurrently selected chain ID
datavoidChain switching result data
variables{ chainId: string } | undefinedLast used chain switching variables
errorError | nullChain switching error if any
isErrorbooleanWhether there's an error
isIdlebooleanWhether the hook is idle
isPendingbooleanWhether chain switching is in progress
isSuccessbooleanWhether chain switching was successful

Released under the MIT License.