Skip to content

useBalance()

Use it to retrieve the ℏ (Hbar) balance of the connected account.

Usage

When parameter multiSession={false}:

tsx
import { useBalance } from '@buidlerlabs/hashgraph-react-wallets'

const App = () => {
  const { data: balance } = useBalance()

  return <span>{balance.formatted ?? '-'}</span>
}

Parameters

ts
interface IUseBalanceProps<Connector> {
  connector?: Connector | null
  autoFetch?: boolean
}

- connector

  • Type: HWBridgeConnector
  • Required: false

Request the balance for a specific wallet when multiSession={true}

tsx
import { HashpackConnector } from '@buidlerlabs/hashgraph-react-wallets/connectors'
import { useBalance } from '@buidlerlabs/hashgraph-react-wallets'

const App = () => {
  const { data: balance } = useBalance({ connector: HashpackConnector }) 

  return <span>{balance.formatted ?? '-'}</span>
}

- autoFetch

  • Type: boolean
  • Required: false
tsx
import { useWallet, useBalance } from '@buidlerlabs/hashgraph-react-wallets'

const App = () => {
  const { isConnected } = useWallet()
  const { data: balance } = useBalance({ autoFetch: isConnected }) 

  return <span>{balance.formatted ?? '-'}</span>
}

Return Type

ts
type UserBalanceResult = {
  decimals: number
  formatted: string
  symbol: string
  value: BigInt | number
}

Returns a TanStack query result where TData is UserBalanceResult

ts
type UseQueryResult<TData, Error>