Skip to content

Commit

Permalink
fix(ui-react): prevent re-calculate friendly address on wallet unchan…
Browse files Browse the repository at this point in the history
…ged in useTonAddress
  • Loading branch information
thekiba committed Apr 21, 2024
1 parent a881c7f commit dfbe5ba
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/ui-react/src/hooks/useTonAddress.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { CHAIN, toUserFriendlyAddress } from '@tonconnect/ui';
import { useTonWallet } from './useTonWallet';
import { useMemo } from 'react';

/**
* Use it to get user's current ton wallet address. If wallet is not connected hook will return empty string.
* @param [userFriendly=true] allows to choose format of the address.
*/
export function useTonAddress(userFriendly = true): string {
const wallet = useTonWallet();

if (wallet) {
return userFriendly
? toUserFriendlyAddress(wallet.account.address, wallet.account.chain === CHAIN.TESTNET)
: wallet.account.address;
} else {
return '';
}
return useMemo(() => {
if (wallet) {
return userFriendly
? toUserFriendlyAddress(
wallet.account.address,
wallet.account.chain === CHAIN.TESTNET
)
: wallet.account.address;
} else {
return '';
}
}, [wallet, userFriendly, wallet?.account.address, wallet?.account.chain]);
}

0 comments on commit dfbe5ba

Please sign in to comment.