Skip to content

fix: track useTechNews mount state via ref + abort stale fetches (#144) - #153

Open
vedant7007 wants to merge 1 commit into
AshutoshDash1999:masterfrom
vedant7007:fix/use-tech-news-stale-fetch-144
Open

fix: track useTechNews mount state via ref + abort stale fetches (#144)#153
vedant7007 wants to merge 1 commit into
AshutoshDash1999:masterfrom
vedant7007:fix/use-tech-news-stale-fetch-144

Conversation

@vedant7007

Copy link
Copy Markdown
Contributor

Summary

useTechNews.refreshNews() was calling fetchNews(true, true) — the second arg (isMounted) was hardcoded to true at the call site, so a refresh triggered right before unmount kept writing state, and two rapid refreshes could race such that the older, slower fetch overwrote the newer one when it finally resolved.

What changed

  • swapped the param-passed isMounted for a hook-level useRef that flips to false in the effect cleanup — same pattern used by useBrowserBookmarks.ts in this repo
  • added an AbortController ref: a new fetch aborts the in-flight one so the newest request always wins under bursty taps or slow networks
  • silently swallow the AbortError in the catch so a superseded fetch doesn't flash the UI into error state
  • wrapped both fetchNews and refreshNews in useCallback so consumers get stable identities and don't rebind effects on every render

Test plan

  • npm run typecheck clean
  • npx eslint src/hooks/use-tech-news.ts clean
  • confirmed old code path — refresh + navigate away would have still called setNews on unmounted component
  • confirmed no other files import fetchNews directly — greppable via grep -rn "fetchNews" src
  • manual repro once maintainer approves: rapid-refresh test, confirm no unmounted-setState warnings

Fixes #144

…abort (AshutoshDash1999#144)

refreshNews previously called `fetchNews(true, true)` — the second arg
(isMounted) was hardcoded true at the call site, so a refresh triggered
right before unmount still wrote state, and a second refresh fired
before the first resolved could be overwritten by the slower older
request when it finally landed.

- track mount state via a hook-level useRef and flip it in the effect
  cleanup, matching the mount-guard pattern already used in
  use-browser-bookmarks.ts
- keep an AbortController ref so a new refresh cancels the in-flight
  fetch, guaranteeing the newest request wins even under bursty user
  taps or slow networks; silently swallow the resulting AbortError so
  the UI doesn't flash an error state on a superseded request
- wrap fetchNews and refreshNews in useCallback for stable identity so
  consumers don't rebind effects on every render

Fixes AshutoshDash1999#144
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.

[Data-integrity] useTechNews refreshNews() passes hardcoded isMounted=true — stale fetches overwrite fresh data

1 participant