Skip to main content

useBalance

useBalance is a React hook that provides functionality to fetch and monitor SUI token balances for the currently connected wallet address.

Usage

import { useBalance } from '@suiware/kit';

function MyComponent() {
const { balance, error, refetch } = useBalance({
autoRefetch: true,
autoRefetchInterval: 3000
});

return <div>Balance: {balance} SUI</div>;
}

Parameters

The hook accepts an optional configuration object with the following properties:

ParameterTypeDescriptionDefault
autoRefetchbooleanWhether to automatically refresh the balance periodicallyfalse
autoRefetchIntervalnumberInterval for auto-refresh in milliseconds3000

Return Value

PropertyTypeDescription
balancestring | undefinedThe formatted SUI balance
errorError | nullAny error that occurred during fetching
refetch() => voidFunction to manually refresh the balance

Features

  • Fetches SUI balance for the current wallet address

  • Supports one-time and periodic balance updates

  • Automatically formats balance values

  • Provides error handling

  • Includes manual refresh capability

  • Cleans up intervals on unmount

Usage Examples

One-time Balance Fetch

const { balance } = useBalance({
autoRefetch: false
});

Regular Balance Updates

const { balance } = useBalance({
autoRefetch: true,
autoRefetchInterval: 5000 // Updates every 5 seconds
});

Manual Balance Refresh

const { balance, refetch } = useBalance();

// Later in your code
const handleRefresh = () => {
refetch();
};