useTokenAccounts
Fetch all SPL token accounts owned by a Solana address.
Import
import { useTokenAccounts } from '@wankmi/wankmi'Usage
import { useWallet, useTokenAccounts } from '@wankmi/wankmi'
function TokenList() {
const { publicKey } = useWallet()
const { data: accounts, isLoading } = useTokenAccounts({ owner: publicKey })
if (isLoading) return <p>Loading tokens...</p>
return (
<ul>
{accounts?.map((account) => (
<li key={account.pubkey.toBase58()}>
{account.account.data.parsed.info.mint} —{' '}
{account.account.data.parsed.info.tokenAmount.uiAmountString}
</li>
))}
</ul>
)
}Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
owner | PublicKey | null | undefined | No | The owner address. Query disabled when null or undefined. |
Return Value
| Property | Type | Description |
|---|---|---|
data | ParsedAccountData[] | undefined | Array of parsed token accounts |
isLoading | boolean | True on the first fetch |
isError | boolean | True if the fetch failed |
refetch | () => void | Manually trigger a refetch |
💡
This hook uses getParsedTokenAccountsByOwner under the hood and returns fully parsed account data including mint address, token amount, and decimals.