BluxBlux

useAccounts

Fetch a filtered list of Stellar accounts in your React app using the Blux SDK.

The useAccounts hook returns a paginated list of Stellar accounts matching the given filters. At least one filter — forSigner, forAsset, sponsor, or forLiquidityPool — must be provided.

Import

import { useAccounts } from "@bluxcc/react";

Usage

Filter by asset to fetch all accounts holding a specific token:

import { useAccounts, Asset } from "@bluxcc/react";

function App() {
  const { data } = useAccounts({
    forAsset: new Asset("USDC", "GA5Z...KZVN")
  });
}
root.render(
  <BluxProvider
    config={{
      appName: "MyApp",
      networks: [networks.testnet, networks.mainnet],
    }}
  >
    <App />
  </BluxProvider>
);

Combine with useBlux to fetch accounts only when the user is authenticated:

import { useAccounts, useBlux } from "@bluxcc/react";

function App() {
  const { user, isAuthenticated } = useBlux();
  const { data } = useAccounts(
    { forSigner: user.address },
    { enabled: isAuthenticated }
  );
}
root.render(
  <BluxProvider
    config={{
      appName: "MyApp",
      networks: [networks.testnet, networks.mainnet],
    }}
  >
    <App />
  </BluxProvider>
);

Pass TanStack Query options as a second argument to customize caching and refetch behavior:

import { useAccounts, Asset } from "@bluxcc/react";

function App() {
  const { data } = useAccounts(
    { forAsset: new Asset("USDC", "GA5Z...KZVN") },
    { retry: 3, staleTime: 60000 }
  );
}
root.render(
  <BluxProvider
    config={{
      appName: "MyApp",
      networks: [networks.testnet, networks.mainnet],
    }}
  >
    <App />
  </BluxProvider>
);

Always pass core parameters first, then TanStack Query options. The hook relies on this order to work correctly.

Parameters

forSigner

string | undefined

Filter accounts that list the given address as a signer.

forAsset

Asset | undefined

Filter accounts that hold the specified asset. Import Asset from @bluxcc/react.

string | undefined

Filter accounts sponsored by the specified account ID.

forLiquidityPool

string | undefined

Filter accounts related to a specific liquidity pool ID.

cursor

string | undefined

Pagination cursor for fetching the next or previous page.

limit

number | undefined

Number of records to return per page.

network

string | undefined

The network to query. Omit to use the active network.

order

'asc' | 'desc' | undefined

Sort order for the results.

Query Options

OptionTypeDefaultDescription
enabledbooleantrueSet to false to disable automatic fetching
staleTimenumber | Infinity0Time in ms before data is considered stale
gcTimenumber | Infinity300000Time in ms before inactive cache data is garbage collected
retryboolean | number3How many times to retry on failure
retryDelaynumber | functionDelay in ms between retry attempts
refetchIntervalnumber | false | functionContinuously refetch at this interval in ms
refetchOnMountboolean | 'always'trueRefetch on component mount if data is stale
refetchOnWindowFocusboolean | 'always'trueRefetch when the window regains focus
refetchOnReconnectboolean | 'always'trueRefetch when network reconnects
refetchIntervalInBackgroundbooleanKeep refetching even when tab is in background
placeholderDataGetAccountsResult | functionPlaceholder data shown while query is pending (not persisted to cache)
initialDataGetAccountsResult | functionInitial data for the cache (persisted)
initialDataUpdatedAtnumber | functionTimestamp of when initialData was last updated
selectfunctionTransform or select a subset of the returned data
notifyOnChangePropsstring[] | 'all'Limit re-renders to specific property changes
structuralSharingboolean | functiontrueRetain references from old data for performance
networkMode'online' | 'always' | 'offlineFirst''online'Controls when queries can run relative to network status
metaRecord<string, unknown>Attach arbitrary metadata to the query cache entry
queryClientQueryClientUse a custom QueryClient instead of the nearest context one

Return Type

data

GetAccountsResult | undefined

type GetAccountsResult = {
  builder: AccountCallBuilder; // .next() and .prev() for pagination
  response: Horizon.ServerApi.CollectionPage<Horizon.ServerApi.AccountRecord>;
};

Status Booleans

PropertyTypeDescription
isPendingbooleanNo cached data and no completed fetch yet
isSuccessbooleanQuery resolved successfully
isErrorbooleanQuery failed
isLoadingbooleanFirst fetch in-flight (isFetching && isPending)
isFetchingbooleanQuery function is currently executing
isRefetchingbooleanBackground refetch in progress
isFetchedbooleanQuery has been fetched at least once
isFetchedAfterMountbooleanQuery fetched after component mounted
isStalebooleanCached data is stale or older than staleTime
isPlaceholderDatabooleanCurrently showing placeholder data
isLoadingErrorbooleanFailed on the first fetch
isRefetchErrorbooleanFailed during a background refetch
isPausedbooleanQuery wanted to fetch but was paused

Other Returns

PropertyTypeDescription
status'pending' | 'success' | 'error'Current query status
fetchStatus'fetching' | 'idle' | 'paused'Current fetch status
errornull | ErrorError object if the query failed
dataUpdatedAtnumberTimestamp of last successful fetch
errorUpdatedAtnumberTimestamp of last error
errorUpdateCountnumberTotal number of errors
failureCountnumberFailures since last success
failureReasonnull | ErrorReason for last retry failure
refetchfunctionManually trigger a refetch

On this page