Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/useaccount-default-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/react-hooks": patch
---

Fix useAccount hook to default `fetch` and `watch` options to `true`, matching the behavior of useBalance. This ensures account data is fetched and watched automatically without requiring explicit options.
30 changes: 26 additions & 4 deletions packages/react-hooks/src/hooks.account.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,44 @@ describe('account hooks', () => {
expect(subscription?.abort).toHaveBeenCalledTimes(1);
});

it('skips fetches when disabled and when no address is provided', async () => {
it('defaults to fetch and watch enabled', async () => {
const address = createAddress(2);
const { client, unmount } = renderHookWithClient(() => useAccount(address));

// Should fetch by default
await waitFor(() => {
expect(client.actions.fetchAccount).toHaveBeenCalledWith(address, undefined);
});

// Should watch by default
expect(client.watchers.watchAccount).toHaveBeenCalledWith(
{ address, commitment: undefined },
expect.any(Function),
);

const subscription = client.watchers.watchAccount.mock.results[0]?.value;
unmount();
expect(subscription?.abort).toHaveBeenCalledTimes(1);
});

it('skips fetches when disabled and when no address is provided', async () => {
const address = createAddress(3);
const { client, result } = renderHookWithClient(() => useAccount(undefined, { commitment: 'confirmed' }));
expect(result.current).toBeUndefined();
expect(client.actions.fetchAccount).not.toHaveBeenCalled();
expect(client.watchers.watchAccount).not.toHaveBeenCalled();

const { client: clientWithSkip } = renderHookWithClient(() => useAccount(address, { fetch: false }));
const { client: clientWithSkip } = renderHookWithClient(() =>
useAccount(address, { fetch: false, watch: false }),
);
await waitFor(() => {
expect(clientWithSkip.actions.fetchAccount).not.toHaveBeenCalled();
});
expect(clientWithSkip.watchers.watchAccount).not.toHaveBeenCalled();
});

it('tracks lamport balances and watcher state', async () => {
const address = createAddress(3);
const address = createAddress(4);
const entry = createAccountEntry({
address,
fetching: true,
Expand Down Expand Up @@ -102,7 +124,7 @@ describe('account hooks', () => {
});

it('respects skip options when monitoring balances', async () => {
const address = createAddress(4);
const address = createAddress(5);
const { client } = renderHookWithClient(() => useBalance(address, { fetch: false, watch: false }));

await waitFor(() => {
Expand Down
23 changes: 16 additions & 7 deletions packages/react-hooks/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,16 @@ export function useSplToken(
*/
export function useAccount(addressLike?: AddressLike, options: UseAccountOptions = {}): AccountCacheEntry | undefined {
const client = useSolanaClient();
const shouldSkip = options.skip ?? !addressLike;
const mergedOptions = useMemo(
() => ({
commitment: options.commitment,
fetch: options.fetch ?? true,
skip: options.skip,
watch: options.watch ?? true,
}),
[options.commitment, options.fetch, options.skip, options.watch],
);
const shouldSkip = mergedOptions.skip ?? !addressLike;
const { address, addressError } = useMemo(() => {
if (shouldSkip || !addressLike) {
return { address: undefined, addressError: undefined };
Expand All @@ -640,12 +649,12 @@ export function useAccount(addressLike?: AddressLike, options: UseAccountOptions
const account = useClientStore(selector);

useSuspenseFetcher({
enabled: options.fetch !== false && !shouldSkip && Boolean(address),
enabled: mergedOptions.fetch !== false && !shouldSkip && Boolean(address),
fetcher: () => {
if (!address) {
throw new Error('Provide an address before fetching account data.');
}
return client.actions.fetchAccount(address, options.commitment);
return client.actions.fetchAccount(address, mergedOptions.commitment);
},
key: accountKey ?? null,
ready: account !== undefined,
Expand All @@ -655,18 +664,18 @@ export function useAccount(addressLike?: AddressLike, options: UseAccountOptions
if (!address) {
return;
}
const commitment = options.commitment;
if (options.fetch !== false && account === undefined) {
const commitment = mergedOptions.commitment;
if (mergedOptions.fetch !== false && account === undefined) {
void client.actions.fetchAccount(address, commitment).catch(() => undefined);
}
if (options.watch) {
if (mergedOptions.watch) {
const subscription = client.watchers.watchAccount({ address, commitment }, () => undefined);
return () => {
subscription.abort();
};
}
return undefined;
}, [account, address, client, options.commitment, options.fetch, options.watch]);
}, [account, address, client, mergedOptions.commitment, mergedOptions.fetch, mergedOptions.watch]);

return account;
}
Expand Down
Loading