Sub topic 16
Choosing an approach
This chapter assumes you know useEffect and cleanup, the useFetch hook pattern, TanStack Query basics, and what Suspense and Server Components do. Here you compare the options and pick one for a given situation.
The three options
React gives you three places to fetch data. Each fits a different kind of problem.
- Effect-based fetching runs in the browser after render. You write the loading, error, and cancellation logic yourself, or wrap it in a hook like
useFetch. - A server-state library such as TanStack Query also runs in the browser. It adds a cache, request deduplication, refetching, and mutations.
- Framework-level fetching with Server Components runs on the server before the HTML reaches the browser. The component awaits the data directly, with no effect and no client state.
Comparing them
| Concern | Effect or `useFetch` | TanStack Query | Server Components |
|---|---|---|---|
| Where it runs | Browser | Browser | Server |
| Cache shared across components | No | Yes, by query key | Depends on the framework |
| Duplicate requests deduplicated | No | Yes | Depends on the framework |
| Refetch on focus or reconnect | You write it | Built in | Not applicable |
| Updating server data | You write it | `useMutation` and invalidation | Framework actions or API calls |
| Extra dependency | None | One library | A supporting framework |
A decision flow
Rules of thumb
- Use Server Components when the framework offers them and the data is needed on first render. The user gets content without waiting for JavaScript to load and then fetch.
- Use TanStack Query for client-side data that is shared, changes often, or is edited by the user. Typical cases are dashboards, lists with filters, and forms that update records.
- Use a plain effect or
useFetchfor small, isolated cases. A single read-only request in a small app does not justify a new dependency.
These options can be combined. A Server Component can render the first page of a list, and a client component using TanStack Query can handle filtering and updates afterward.
Warning signs
Watch for signs that you have outgrown your current approach:
- You copy the same loading and error code into many components.
- Two components request the same URL and show different values.
- You add manual caching, retry, or refetch logic to
useFetch. - You pass fetched data through many props only to avoid refetching.
When you see these signs, move to a library or to server-side fetching. Do not keep extending the hook.
Exercise
A product page shows a catalog that is read-only, needed immediately on first load, and the app uses a framework with Server Components. A separate admin screen lets users edit items, and several components display the same item. Which approach fits each screen, and why?
Show answer
Use a Server Component for the catalog page. The data is read-only and needed at first render, so fetching on the server avoids client loading states and extra JavaScript work. Use TanStack Query for the admin screen. Several components share the same item, so a query key gives them one cached copy. After an edit, useMutation with invalidateQueries refreshes every component that shows that item.
Exercise
Your useFetch hook now has retry logic, a manual cache object, and a refetch timer. What does this suggest, and what should you do?
Show answer
The hook is turning into a partial server-state library. Replace it with TanStack Query, which provides caching, retries, and refetching and has been tested against many edge cases.