Managing/Listening to on chain network changes

Hello all,

So Iā€™m having a hard time grasping the idea of watching a user change networks. So my app does the authenticate correctly, I read that authenticate also enables web3 (correct me if i am wrong). Now that the user is authenticated, how do I go about watching the network switching ?

  • can you please show in react code, the imports, hooks, etc that I will need.

Thank you

The onChainChanged is what you need.

Moralis.onChainChanged(function(chain) {
  console.log(chain);
});

https://docs.moralis.io/moralis-server/web3/web3#events