diff --git a/packages/extension-polkagate/src/hooks/useAssetsBalances.ts b/packages/extension-polkagate/src/hooks/useAssetsBalances.ts index 9ff4ddd03..4e1850405 100644 --- a/packages/extension-polkagate/src/hooks/useAssetsBalances.ts +++ b/packages/extension-polkagate/src/hooks/useAssetsBalances.ts @@ -317,7 +317,7 @@ export default function useAssetsBalances (accounts: AccountJson[] | null, setAl return; } - worker.addEventListener('message', (messageEvent: MessageEvent) => { + const handleMessage = (messageEvent: MessageEvent) => { const message = messageEvent.data; if (!message) { @@ -384,7 +384,13 @@ export default function useAssetsBalances (accounts: AccountJson[] | null, setAl console.log('_assets :::', _assets); combineAndSetAssets(_assets); - }); + }; + + worker.addEventListener('message', handleMessage); + + return () => { + worker.removeEventListener('message', handleMessage); + }; }, [combineAndSetAssets, handleRequestCount, worker]); const fetchAssetOnRelayChain = useCallback((_addresses: string[], chainName: string) => { diff --git a/packages/extension-polkagate/src/hooks/useNFT.tsx b/packages/extension-polkagate/src/hooks/useNFT.tsx index c355b5794..49250133b 100644 --- a/packages/extension-polkagate/src/hooks/useNFT.tsx +++ b/packages/extension-polkagate/src/hooks/useNFT.tsx @@ -37,7 +37,7 @@ export default function useNFT (accountsFromContext: AccountJson[] | null) { setFetching(true); worker.postMessage({ functionName: NFT_FUNCTION_NAME, parameters: { addresses } }); - worker.addEventListener('message', (messageEvent: MessageEvent) => { + const handleMessage = (messageEvent: MessageEvent) => { const NFTs = messageEvent.data; if (!NFTs) { @@ -68,7 +68,13 @@ export default function useNFT (accountsFromContext: AccountJson[] | null) { saveToStorage(parsedNFTsInfo.results); // setFetching(false); - }); + }; + + worker.addEventListener('message', handleMessage); + + return () => { + worker.removeEventListener('message', handleMessage); + }; }, [notify, saveToStorage, t, worker]); useEffect(() => {