From b43592364b1f7f644e092917db9efb8c5c20192b Mon Sep 17 00:00:00 2001 From: alcercu Date: Thu, 6 Oct 2022 11:49:30 -0500 Subject: [PATCH] fix: network change handling --- src/hooks/notifications-web3.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/hooks/notifications-web3.js b/src/hooks/notifications-web3.js index 51196266..89ed22b8 100644 --- a/src/hooks/notifications-web3.js +++ b/src/hooks/notifications-web3.js @@ -125,17 +125,27 @@ const useNotificationWeb3 = () => { // Notify of network changes. useEffect(() => { - if (!web3Context.networkId) return - if (!network) { - setNetwork(web3Context.networkId) - return - } + const handleChainChange = () => { + if (!web3Context.networkId) return + if (!network) { + setNetwork(web3Context.networkId) + return + } - if (network && network !== web3Context.networkId) { - setNetwork(web3Context.networkId) - notification.info({ - message: 'Network Changed' - }) + if (network && network !== web3Context.networkId) { + setNetwork(web3Context.networkId) + notification.info({ + message: 'Network Changed' + }) + } + } + const { ethereum } = window + if (ethereum && ethereum.on) { + ethereum.on('chainChanged', handleChainChange) + return () => { + if (ethereum.removeListener) + ethereum.removeListener('chainChanged', handleChainChange) + } } }, [web3Context.networkId, network, TCR2_ADDRESS, history])