From e034aa647a978a001a8aefe4dec4e9e0eaf8dc73 Mon Sep 17 00:00:00 2001 From: Matthew Russell Date: Thu, 14 Sep 2023 15:38:57 -0700 Subject: [PATCH] fix: only subscribe to store once --- .../lib/use-ethereum-transaction-toasts.tsx | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/libs/web3/src/lib/use-ethereum-transaction-toasts.tsx b/libs/web3/src/lib/use-ethereum-transaction-toasts.tsx index f0e49b482..0e0c365b4 100644 --- a/libs/web3/src/lib/use-ethereum-transaction-toasts.tsx +++ b/libs/web3/src/lib/use-ethereum-transaction-toasts.tsx @@ -1,4 +1,5 @@ import type { ReactNode } from 'react'; +import { useEffect } from 'react'; import { useAssetsDataProvider } from '@vegaprotocol/assets'; import { EtherscanLink } from '@vegaprotocol/environment'; import { formatNumber, toBigNum } from '@vegaprotocol/utils'; @@ -218,12 +219,19 @@ export const useEthereumTransactionToasts = () => { [onClose] ); - useEthTransactionStore.subscribe( - (state) => compact(state.transactions.filter((tx) => tx?.dialogOpen)), - (txs) => { - txs.forEach((tx) => { - setToast(fromEthTransaction(tx)); - }); - } - ); + // Only register a subscription once + useEffect(() => { + const unsubscribe = useEthTransactionStore.subscribe( + (state) => compact(state.transactions.filter((tx) => tx?.dialogOpen)), + (txs) => { + txs.forEach((tx) => { + setToast(fromEthTransaction(tx)); + }); + } + ); + + return () => { + unsubscribe(); + }; + }, []); };