What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For API reads that represent shared or revisited server data, TanStack Query is usually a better fit than repeating fetch logic in useEffect: it provides keyed caching and query lifecycle state, and lets you configure freshness and retries. That does not mean React forbids fetching in an Effect. Use a framework’s data-loading approach when it fits; keep an Effect for genuine synchronization or a small, isolated fetch that does not need cache management.

Why repeated API-fetching Effects become application work

React defines useEffect as a way to synchronize a component with an external system. Its documentation includes manual data fetching as an example, but also notes the costs of building that flow yourself: requests are not automatically preloaded or cached, components can create request waterfalls, server-rendered HTML may initially contain only a loading state, and responses can arrive out of order.

To prevent an older response from overwriting newer results, a manual Effect may need cleanup logic such as an ignore flag. That solves one race condition, but it does not provide a shared cache, define how long data is fresh, or establish a consistent retry policy. Those responsibilities remain with the application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

React’s guidance is not “never fetch in an Effect.” It says: “If you’re not trying to synchronize with some external system, you probably don’t need an Effect.” For route data, React recommends considering a framework’s built-in fetching mechanism; where that is not a fit, it points to client-side caches such as TanStack Query, SWR, or React Router. React’s useEffect reference and its discussion of alternatives to fetching in Effects explain the distinction.

What TanStack Query changes

TanStack Query models a server read as a query: a query key identifies the requested data, a query function obtains it, and useQuery exposes its lifecycle state. Components asking for the same keyed resource can use the cache rather than each owning a separate fetching lifecycle. The key is therefore part of the data model, not just a label.

Build keys from every input that changes the resource

If a request depends on a user ID, search term, page number, or other variable, include that variable in the query key. Otherwise, requests for different resources can be treated as if they referred to the same cached data. TanStack’s query guide covers query functions, keys, and result states.

Render each query state intentionally

Account for pending, error, and success states rather than treating every non-success render as the same loading screen. Also distinguish “no data has loaded yet” from “existing data is being refreshed.” A refetch can fail while previously available data remains useful; decide whether to keep showing it, show an update warning, or block the view based on what the screen means.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand the defaults before relying on the cache

TanStack Query’s documented defaults are active policy, not a promise that data will never be fetched again. Cached data is considered stale by default; inactive queries are retained for five minutes; and failed queries are retried three times with exponential backoff. These defaults may suit some screens and be wrong for others. The important defaults guide explains their behavior.

  • Freshness: Set staleTime according to how old the data may be before the UI should treat it as stale.
  • Retention: Review how long inactive query data remains available, especially if the app holds many distinct keys.
  • Retries: Decide whether retrying is appropriate for the endpoint and user experience. A retry policy that helps a transient read failure may be unsuitable for an operation with side effects.

A cache hit does not mean “never refetch.” Freshness and the query’s refetch triggers determine when another request can happen; choose them as part of the feature’s behavior.

Check request waterfalls separately

Replacing an Effect with useQuery does not automatically make a serial dependency parallel. If one request must finish before another can begin, the second still waits. Nested components can also create a waterfall when each starts its request only after rendering.

For independent requests, start them in parallel. For data needed predictably during navigation, consider prefetching. For server-rendered routes, TanStack documents a prefetch, dehydrate, and hydrate workflow, but it should fit the application’s framework and rendering architecture rather than be added by default. See the request waterfalls guide for dependent and nested query patterns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the fetching approach that matches the app

Situation Approach to consider Why
Route data in a framework that already provides loaders or server-data caching Start with the framework’s mechanism It may already handle route-level loading, preloading, or rendering integration; adding a separate cache can duplicate responsibilities.
Client-side server data reused across components or revisited TanStack Query Query keys and cache state provide a shared model for the resource and its lifecycle.
One small, isolated fetch with no meaningful cache or reuse requirement A manual Effect can be reasonable A library may add more structure than the feature needs, provided cleanup and loading/error behavior are handled.
Synchronization with an external system, such as a subscription or imperative API useEffect Effects are designed for synchronizing a component with external systems; this is not merely a server-state caching problem.

Before adopting a client cache, ask whether the same data is shared across components or visits, what age of data is acceptable, whether the app already has route loaders or server rendering, whether requests are independent, and what loading and failure behavior the interface requires. Also consider whether the team wants the additional configuration and query-key conventions.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Adopt it without creating two sources of truth

  1. Check the project’s data architecture. Identify framework loaders, server rendering, and any existing cache before adding a second data-fetching layer.
  2. Install the package. TanStack’s installation guide lists npm i @tanstack/react-query and equivalent commands for pnpm, yarn, bun, and deno. Confirm compatibility for the exact project environment; the current React documentation identifies v5 and states compatibility with React 18 or later, ReactDOM, and React Native. See installation instructions.
  3. Define a query around the resource. Use a stable key containing every changing input that affects the result, and a query function that fetches that resource.
  4. Design the UI states. Handle initial pending, error, and success states, and determine how existing data should appear during a refetch.
  5. Set policy deliberately. Choose freshness, inactive retention, and retry behavior for the API and screen instead of assuming the defaults are right.
  6. Look for serial requests. Use the browser Network panel and the query dependency structure to find requests that could run in parallel or be prefetched.

Do not copy query data into component state simply to make it editable unless there is an explicit synchronization design. Otherwise, the cache and local state can become competing versions of the same server value.

The documentation links above use TanStack’s latest React docs, which are version-sensitive. The current docs identify v5; check the versioned documentation and migration guidance for the project before upgrading or adopting a different major version.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.