Skip to content

Fix/use async initial state refetch#326

Merged
Jagadeeshftw merged 5 commits into
AnchorNet-Org:mainfrom
egwujiohaifesinachiperpetual-max:fix/use-async-initial-state-refetch
Jul 23, 2026
Merged

Fix/use async initial state refetch#326
Jagadeeshftw merged 5 commits into
AnchorNet-Org:mainfrom
egwujiohaifesinachiperpetual-max:fix/use-async-initial-state-refetch

Conversation

@egwujiohaifesinachiperpetual-max

Copy link
Copy Markdown
Contributor

Closes #266
Fixes an issue where useAsync always executed its initial fetch effect on mount, even when seeded with { status: "ready", data: ... } initialState.

In detail pages such as AnchorDetail (/anchors/[id]) and SettlementDetail (/settlements/[id]), passing server-fetched initialData into useAsync previously triggered an immediate redundant client-side re-fetch of the exact same anchor or settlement immediately after rendering.

This PR updates useAsync to skip calling load during mount when nonce === 0 and initialState.status === "ready". Subsequent explicit re-fetches via reload() or refresh() continue to trigger re-fetches as expected.

Changes Made

  • src/hooks/useAsync.ts: Added an early return condition to the useEffect when nonce === 0 && initialState.status === "ready". Updated JSDoc documentation.
  • src/hooks/useAsync.test.ts: Added unit tests asserting:
    • load is skipped on mount when initialized with status: "ready".
    • Calling reload() or refresh() after initialization with "ready" state triggers load.
  • src/components/AnchorDetail.test.tsx: Added unit tests verifying initialData skips fetchAnchor on mount and re-fetches on deactivation reload.
  • src/components/SettlementDetail.test.tsx: Added unit tests verifying initialData skips fetchSettlement on mount and re-fetches on execution refresh.

Verification

  • Unit tests passed across useAsync.test.ts, AnchorDetail.test.tsx, and SettlementDetail.test.tsx.
  • Verified TypeScript type checking via npx tsc --noEmit.

# Conflicts:
#	src/components/SettlementTable.test.tsx
#	src/components/SettlementTable.tsx
#	src/components/SettlementsPanel.test.tsx
Retry logic in apiRequest/apiTextRequest called isAbortError(err) on
every retry-eligible failure, but the function didn't exist anywhere
in the module -- a pre-existing bug on main that threw a
ReferenceError instead of checking abort status.
@Jagadeeshftw
Jagadeeshftw merged commit 4b5a532 into AnchorNet-Org:main Jul 23, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useAsync's initialState is immediately discarded by a redundant client-side refetch on mount

2 participants