Skip to content

watchAccount

Subscribe to account changes.

Import

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

Usage

ts
import { watchAccount } from '@wagmi/core'
import { config } from './config'

const unwatch = watchAccount(config, {
  onChange(data) {
    console.log('Account changed!', data)
  },
})
unwatch()
import { watchAccount } from '@wagmi/core'
import { config } from './config'

const unwatch = watchAccount(config, {
  onChange(data) {
    console.log('Account changed!', data)
  },
})
unwatch()
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 WatchAccountParameters } from '@wagmi/core'
import { type WatchAccountParameters } from '@wagmi/core'

onChange

onChange(account: GetAccountReturnType, prevAccount: GetAccountReturnType): void

Callback function called when account changes.

ts
import { watchAccount } from '@wagmi/core'
import { config } from './config'

const unwatch = watchAccount(config, {
  onChange(account) { 
    console.log('Account changed!', account)
  },
})
unwatch()
import { watchAccount } from '@wagmi/core'
import { config } from './config'

const unwatch = watchAccount(config, {
  onChange(account) { 
    console.log('Account changed!', account)
  },
})
unwatch()
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 WatchAccountReturnType } from '@wagmi/core'
import { type WatchAccountReturnType } from '@wagmi/core'

Function for cleaning up watcher.

Released under the MIT License.