Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Memoize bridge props to avoid triggering effect logic on each render #11201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
import { WalletProvider as TronWalletProvider } from '@tronweb3/tronwallet-adapter-react-hooks'
import { env } from '../../configs'

const EMPTY_ARRAY: never[] = []

export function BridgeWalletProvider(props: React.PropsWithChildren) {
const { children } = props

return (
<SolanaConnectionProvider endpoint={env.SOLANA_RPC_ENDPOINT}>
<SolanaWalletProvider wallets={[]} autoConnect={false}>
<TronWalletProvider adapters={[]} autoConnect={false}>
<SolanaWalletProvider wallets={EMPTY_ARRAY} autoConnect={false}>
<TronWalletProvider adapters={EMPTY_ARRAY} autoConnect={false}>
{children}
</TronWalletProvider>
</SolanaWalletProvider>
Expand Down
17 changes: 11 additions & 6 deletions packages/canonical-bridge/src/views/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTranslation } from '@pancakeswap/localization'
import { Flex, useToast } from '@pancakeswap/uikit'
import { useMemo } from 'react'
import { useCallback, useMemo } from 'react'

import {
BridgeRoutes,
Expand Down Expand Up @@ -67,6 +67,15 @@ export const CanonicalBridge = (props: CanonicalBridgeProps) => {
.map((chain) => ({ ...chain, rpcUrl: props.rpcConfig?.[chain.id]?.[0] ?? chain.rpcUrl }))
}, [supportedChainIds, connector?.id, props.rpcConfig])

const handleError = useCallback(
(params: { type: string; message?: string | undefined; error?: Error | undefined }) => {
if (params.message) {
toast.toastError(params.message)
}
},
[toast],
)

return (
<BridgeWalletProvider>
<GlobalStyle />
Expand All @@ -76,11 +85,7 @@ export const CanonicalBridge = (props: CanonicalBridgeProps) => {
chains={supportedChains}
connectWalletButton={connectWalletButton}
refreshingIcon={<RefreshingIcon />}
onError={(params) => {
if (params.message) {
toast.toastError(params.message)
}
}}
onError={handleError}
>
<Flex flexDirection="column" justifyContent="center" maxWidth="480px" width="100%">
<BridgeTransfer />
Expand Down
Loading