1

I am trying to listen to the chainChanged event for Metamask, but the event never fires. When running the following snippet, I get a console.log of the chainId once, but the chainChanged event never runs. What could be the issue? Thanks!

if (window.ethereum) {
  window.ethereum.send('eth_chainId')
        .then(handleChainChanged)
        .catch((err: any) => console.error(err));
    
  window.ethereum.on('chainChanged', handleChainChanged);

  window.ethereum.autoRefreshOnNetworkChange = false;
  ...
}
    
function handleChainChanged(chainId: number) {
  console.log(chainId);
}

1 Answer 1

1

Current Answer

Your question now contains the valid answer. Use chainChanged event instead that specified in EIP-1193 (also in MetaMask)

window.ethereum.on('chainChanged', handleChainChanged);

The networkChanged event has been deprecated in EIP-1193 (also in MetaMask)

Original Answer from 2020

Try window.ethereum.on('networkChanged', handleChainChanged)

From what I know, the new MetaMask api (which will change this command to chainChanged) is not yet live.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.