refactor: extract shared useFetch hook for Projects, Profile and Blogs#851
Open
kapeleshh wants to merge 2 commits into
Open
refactor: extract shared useFetch hook for Projects, Profile and Blogs#851kapeleshh wants to merge 2 commits into
kapeleshh wants to merge 2 commits into
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The Projects, Profile and Blogs sections each duplicated the same
fetch → check ok → parse JSON → catch/loglogic, using an"Error"string as a state sentinel. This PR extracts that into a single reusable, unit-testedsrc/hooks/useFetchhook returning{data, error, isLoading}, and refactors the three sections to consume it.User-facing behaviour (loading / empty / error render states) is unchanged.
Why
useFetchis unit-tested, whereas the previous inline logic was not.useLocalStoragehook, so it fits the current structure.Improvements bundled in
if (flag === "true") { fetch... }wrappers inProfileandBlogswithuseFetch(flag ? url : null).AbortController+ anactiveguard, removing a potential "setState after unmount".lazy()import inProjectsout of the render body (it was recreated on every render) and drops the stale// todo: remove useContexcomment.Tests
src/hooks/useFetch.test.jscovering the success / skipped-request / error paths.App.test.jsfailure: its module-scopewindow.matchMediajest.fn()was wiped by CRA's defaultresetMocksbefore the test ran, soMaincrashed readingdarkPref.matches. The stub is moved tosetupTests.jsas a plain function (untouched byresetMocks) shared by all tests. The suite now passes (4/4).Type of change
App.test.jsuseFetchhook + refactorVerification
npm test→ 4 passing, 2 suitesnpm run build→ compiles successfullynpm run check-format→ all files conform