WAGMI Issue #4879
-
|
Wallet address not updating after switching accounts in Wagmi v1 with MetaMask I’m using useAccount() and useConnect() along with RainbowKit. The wallet connects fine, but if I switch accounts inside MetaMask, the UI still shows the old address and doesn’t update. Is there a way to subscribe to account change events in Wagmi v1 so the UI refreshes automatically? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
🔍 Why the Account Doesn’t Update 🛠 Working Fix (Subscribe to Account Changes) export function useSyncedAccount() { useEffect(() => { return current 💡 Why This Works By syncing both into a useState() value, the component re-renders on address switching. This allows the UI to track: 🧠 Bonus Tip (Avoid Double-Render Bugs) When using RainbowKit + Wagmi, always avoid storing address in React Context manually. Use Wagmi as the single source of truth and derive data from the watcher. |
Beta Was this translation helpful? Give feedback.
🔍 Why the Account Doesn’t Update
useAccount() only subscribes to connection events, not account change events, when a user switches accounts inside MetaMask or any injected wallet.
So the UI doesn’t refresh because there’s no listener tracking the new address after the first connection.
In Wagmi v1, account switching must be observed manually using watchAccount().
🛠 Working Fix (Subscribe to Account Changes)
`import { useAccount, watchAccount } from 'wagmi'
import { useState, useEffect } from 'react'
export function useSyncedAccount() {
const { address } = useAccount()
const [current, setCurrent] = useState(address)
useEffect(() => {
const unwatch = watchAccount((acc) => {
setCurrent(acc?…