Skip to content

switchAccount

Action for switching the current account.

Import

ts
import { switchAccount } from '@wagmi/core'
import { switchAccount } from '@wagmi/core'

Usage

ts
import { getConnections, switchAccount } from '@wagmi/core'
import { config } from './config'

const connections = getConnections(config)
const result = await switchAccount(config, {
  connector: connections[0]?.connector,
})
import { getConnections, switchAccount } from '@wagmi/core'
import { config } from './config'

const connections = getConnections(config)
const result = await switchAccount(config, {
  connector: connections[0]?.connector,
})
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

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

Parameters

ts
import { type SwitchAccountParameters } from '@wagmi/core'
import { type SwitchAccountParameters } from '@wagmi/core'

connector

Connector

Connector to switch to.

ts
import { getConnections, switchAccount } from '@wagmi/core'
import { config } from './config'

const connections = getConnections(config)
const result = await switchAccount(config, {
  connector: connections[0]?.connector, 
})
import { getConnections, switchAccount } from '@wagmi/core'
import { config } from './config'

const connections = getConnections(config)
const result = await switchAccount(config, {
  connector: connections[0]?.connector, 
})
ts
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

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

Return Type

ts
import { type SwitchAccountReturnType } from '@wagmi/core'
import { type SwitchAccountReturnType } from '@wagmi/core'

accounts

readonly [Address, ...Address[]]

Connected accounts from connector.

chainId

number

Connected chain ID from connector.

Error

ts
import { type SwitchAccountErrorType } from '@wagmi/core'
import { type SwitchAccountErrorType } from '@wagmi/core'

TanStack Query

ts
import {
  type SwitchAccountData,
  type SwitchAccountVariables,
  type SwitchAccountMutate,
  type SwitchAccountMutateAsync,
  switchAccountMutationOptions,
} from '@wagmi/core/query'
import {
  type SwitchAccountData,
  type SwitchAccountVariables,
  type SwitchAccountMutate,
  type SwitchAccountMutateAsync,
  switchAccountMutationOptions,
} from '@wagmi/core/query'

Released under the MIT License.