clawchain/wagmi/site/vue/api/composables/useSwitchChain.md

2.0 KiB

title, description
title description
useSwitchChain Composable for switching the target chain for a connector or the Wagmi `Config`.

useSwitchChain

Composable for switching the target chain for a connector or the Wagmi Config.

Import

import { useSwitchChain } from '@wagmi/vue'

Usage

::: code-group

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

const switchChain = useSwitchChain()
const chains = useSwitchChains()
</script>

<template>
  <div>
    <button 
      v-for="chain in chains" 
      :key="chain.id" 
      @click="switchChain.mutate({ chainId: chain.id })"
    >
      {{ chain.name }}
    </button>
  </div>
</template>

<<< @/snippets/vue/config.ts[config.ts] :::

::: tip When connected, switchChain will switch the target chain for the connector. When not connected, switchChain will switch the target chain for the Wagmi Config. :::

Parameters

import { type UseSwitchChainParameters } from '@wagmi/vue'

config

Config | undefined

Config to use instead of retrieving from the WagmiPlugin.

::: code-group

<script setup lang="ts">
import { useSwitchChain } from '@wagmi/vue'
import { config } from './config' // [!code focus]

const switchChain = useSwitchChain({
  config, // [!code focus]
})
</script>

<<< @/snippets/vue/config.ts[config.ts] :::

Return Type

import { type UseSwitchChainReturnType } from '@wagmi/vue'

Action