Display SOL Balance
Fetch and display the connected wallet's SOL balance, with a refresh button.
Code
Balance.tsx
import { useWallet, useSolBalance } from '@wankmi/wankmi'
export function Balance() {
const { publicKey, connected } = useWallet()
const { data: balance, isLoading, isFetching, refetch } = useSolBalance({
address: publicKey,
})
if (!connected) return <p>Connect your wallet to see your balance.</p>
return (
<div>
<p>
{isLoading
? 'Loading...'
: `${balance?.toFixed(4) ?? '—'} SOL`}
</p>
<button onClick={() => refetch()} disabled={isFetching}>
{isFetching ? 'Refreshing...' : 'Refresh'}
</button>
</div>
)
}Notes
useSolBalanceis disabled whenaddressisnull, so it won't fire before the wallet is connected.balanceis in SOL — use.toFixed(4)or similar for clean display.isFetchingistrueon background refetches, whileisLoadingis onlytrueon the initial fetch. UseisFetchingto show a loading indicator on refresh.