From 33c93b5ded245765ddefcf796b733ed5ce13ab3d Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Mon, 18 Sep 2023 13:08:37 -0700 Subject: [PATCH] fix: spurious Swap re-renders (#7333) --- src/components/Tokens/TokenDetails/index.tsx | 6 ++--- src/pages/Swap/index.tsx | 23 +++++++++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/components/Tokens/TokenDetails/index.tsx b/src/components/Tokens/TokenDetails/index.tsx index b4b798094c..ac4eb0e161 100644 --- a/src/components/Tokens/TokenDetails/index.tsx +++ b/src/components/Tokens/TokenDetails/index.tsx @@ -248,10 +248,8 @@ export default function TokenDetails({
diff --git a/src/pages/Swap/index.tsx b/src/pages/Swap/index.tsx index d0716b211e..bda2feb3c4 100644 --- a/src/pages/Swap/index.tsx +++ b/src/pages/Swap/index.tsx @@ -150,10 +150,8 @@ export default function SwapPage({ className }: { className?: string }) { @@ -172,13 +170,15 @@ export default function SwapPage({ className }: { className?: string }) { */ export function Swap({ className, - prefilledState = {}, + initialInputCurrencyId, + initialOutputCurrencyId, chainId, onCurrencyChange, disableTokenInputs = false, }: { className?: string - prefilledState?: Partial + initialInputCurrencyId?: string | null + initialOutputCurrencyId?: string | null chainId?: ChainId onCurrencyChange?: (selected: Pick) => void disableTokenInputs?: boolean @@ -187,8 +187,8 @@ export function Swap({ const trace = useTrace() // token warning stuff - const prefilledInputCurrency = useCurrency(prefilledState?.[Field.INPUT]?.currencyId) - const prefilledOutputCurrency = useCurrency(prefilledState?.[Field.OUTPUT]?.currencyId) + const prefilledInputCurrency = useCurrency(initialInputCurrencyId) + const prefilledOutputCurrency = useCurrency(initialOutputCurrencyId) const [loadedInputCurrency, setLoadedInputCurrency] = useState(prefilledInputCurrency) const [loadedOutputCurrency, setLoadedOutputCurrency] = useState(prefilledOutputCurrency) @@ -236,6 +236,13 @@ export function Swap({ const toggleWalletDrawer = useToggleAccountDrawer() // swap state + const prefilledState = useMemo( + () => ({ + [Field.INPUT]: { currencyId: initialInputCurrencyId }, + [Field.OUTPUT]: { currencyId: initialOutputCurrencyId }, + }), + [initialInputCurrencyId, initialOutputCurrencyId] + ) const [state, dispatch] = useReducer(swapReducer, { ...initialSwapState, ...prefilledState }) const { typedValue, recipient, independentField } = state