0

I am trying to listen to the 'chainChanged' event for Metamask, but the event never fires. When running the following snippet, the 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

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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