Skip to content

refactor: extract shared useFetch hook for Projects, Profile and Blogs#851

Open
kapeleshh wants to merge 2 commits into
saadpasta:masterfrom
kapeleshh:refactor/use-fetch-hook
Open

refactor: extract shared useFetch hook for Projects, Profile and Blogs#851
kapeleshh wants to merge 2 commits into
saadpasta:masterfrom
kapeleshh:refactor/use-fetch-hook

Conversation

@kapeleshh

Copy link
Copy Markdown

Description

The Projects, Profile and Blogs sections each duplicated the same fetch → check ok → parse JSON → catch/log logic, using an "Error" string as a state sentinel. This PR extracts that into a single reusable, unit-tested src/hooks/useFetch hook returning {data, error, isLoading}, and refactors the three sections to consume it.

User-facing behaviour (loading / empty / error render states) is unchanged.

Why

  • Removes ~140 lines of duplicated fetch/error boilerplate across three components.
  • useFetch is unit-tested, whereas the previous inline logic was not.
  • Lives alongside the existing useLocalStorage hook, so it fits the current structure.

Improvements bundled in

  • Skips the request for a falsy URL — replaces the if (flag === "true") { fetch... } wrappers in Profile and Blogs with useFetch(flag ? url : null).
  • Aborts in-flight requests on unmount via AbortController + an active guard, removing a potential "setState after unmount".
  • Hoists the lazy() import in Projects out of the render body (it was recreated on every render) and drops the stale // todo: remove useContex comment.

Tests

  • Adds src/hooks/useFetch.test.js covering the success / skipped-request / error paths.
  • Fixes a pre-existing App.test.js failure: its module-scope window.matchMedia jest.fn() was wiped by CRA's default resetMocks before the test ran, so Main crashed reading darkPref.matches. The stub is moved to setupTests.js as a plain function (untouched by resetMocks) shared by all tests. The suite now passes (4/4).

Type of change

  • Bug fix (non-breaking change which fixes an issue) — App.test.js
  • New feature (non-breaking change which adds functionality) — useFetch hook + refactor

Verification

  • npm test → 4 passing, 2 suites
  • npm run build → compiles successfully
  • npm run check-format → all files conform

Kapeleshh KS and others added 2 commits June 25, 2026 22:51
The Projects, Profile and Blogs sections each duplicated the same
fetch -> check ok -> parse JSON -> catch/log boilerplate and used an
"Error" string as a state sentinel. Extract it into a reusable
src/hooks/useFetch hook that returns {data, error, isLoading}.

Improvements over the previous code:
- Skips the request when given a falsy URL, replacing the
  `if (flag === "true") { fetch... }` wrappers in Profile and Blogs.
- Aborts in-flight requests on unmount via AbortController plus an
  `active` guard, removing a potential setState-after-unmount.
- Hoists the lazy() import in Projects out of the render body (it was
  recreated on every render) and drops the stale `// todo: remove
  useContex` comment.

Adds src/hooks/useFetch.test.js covering the success, skipped-request
and error paths. User-facing render states are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
App.test defined window.matchMedia as a module-scope jest.fn(). CRA's
default resetMocks wipes mock implementations before each test, so by
the time the test rendered, matchMedia() returned undefined and Main
crashed reading darkPref.matches.

Move the stub to setupTests.js as a plain function (untouched by
resetMocks) so it is shared by every test, and drop the broken mock
from App.test. The suite now passes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Jun 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
developer-folio Error Error Jun 25, 2026 1:55pm

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.

1 participant