Latest metamask recommended connect to wallet method can't follow account change

MetaMask’s current recommended way to connect to wallet (can’t paste link but it’s under get-started > setup dev environment) is supposed to handle account changes, but in practice it doesn’t appear to work. If I connect with one account, then change accounts, the new account isn’t yet connected to the site, and therefore doesn’t seem to trigger the following code (supplied by MetaMask) to call handleAccountsChanged()

window.ethereum.on(‘accountsChanged’, handleAccountsChanged);

function handleAccountsChanged(accounts) {
if (accounts.length === 0) {
console.log(‘Please connect to MetaMask.’);
} else if (accounts[0] !== currentAccount) {
currentAccount = accounts[0];
}
}

1 Like

@VanGogh66 Hello!
Welcome to MetaMask community !

After changing the account, MetaMask asks you if you want to connect the current account to the application, if you agree - the event is triggered

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.