Hooks
useWallet

useWallet

Connect and manage the user's Solana wallet.

Import

import { useWallet } from '@wankmi/wankmi'

Usage

import { useWallet } from '@wankmi/wankmi'
 
function ConnectButton() {
  const { connect, disconnect, connected, publicKey, connecting } = useWallet()
 
  if (connecting) return <button disabled>Connecting...</button>
 
  if (connected) {
    return (
      <div>
        <p>{publicKey?.toBase58()}</p>
        <button onClick={disconnect}>Disconnect</button>
      </div>
    )
  }
 
  return <button onClick={connect}>Connect</button>
}

Return Value

PropertyTypeDescription
connect() => Promise<void>Prompt the user to connect their wallet
disconnect() => Promise<void>Disconnect the active wallet
connectedbooleanWhether a wallet is currently connected
connectingbooleanWhether a connection attempt is in progress
publicKeyPublicKey | nullThe connected wallet's public key
walletWalletAdapter | nullThe active wallet adapter instance

Notes

💡

useWallet must be used inside a WankmiProvider. Calling it outside the provider will throw.

  • connect() will open the wallet's browser extension UI (e.g. Phantom popup). The user must approve the connection.
  • publicKey is null when disconnected.
  • The adapter used is determined by which wallet the user has installed. If multiple adapters are configured, the first detected wallet is used.