Appearance

Visual style

Mode
Theme
Accent
Advanced
Text sizeScales the whole site
16 px
Reduce motionReduce animations or match your system
Separate light and darkChoose a theme, accent and font for each
50

Technology · Frontend

TanStack Query for React dashboards

TanStack Query (formerly React Query) is a data-fetching and server-state library. Its documentation describes caching, deduplication, background refetching, pagination and mutations with invalidation, independent of how the data is fetched (REST, GraphQL or anything that returns a promise).

In practice

How I use TanStack Query

TanStack Query manages server data in the Ekonavi marketplace admin dashboard and admin dashboard (over Hasura GraphQL), in Paisa Vanno's real-time donation tracker alongside tRPC, and in the storefront of a creator commerce platform. Where an app also has Redux Toolkit, Redux is limited to client state such as UI preferences and session.

Query key conventions

I define query keys in one place so the whole app uses the same ones:

export const orderKeys = {
  all: ["orders"] as const,
  list: (f: { status: string; page: number }) => [...orderKeys.all, "list", f] as const,
  detail: (id: string) => [...orderKeys.all, "detail", id] as const,
};

// After a mutation, invalidate every order list but keep unrelated caches warm
queryClient.invalidateQueries({ queryKey: orderKeys.all });

Optimistic updates for operations work

Dashboards for sellers and operators are used all day. For small, reversible actions (mark an order shipped, toggle a product’s visibility), an optimistic update makes the interface feel instant, with automatic rollback if the server rejects the change. For anything involving money, I wait for the server.

Where it sits in the architecture

TanStack Query lives in the container layer, as hooks such as useOrders() and useUpdateStock(). Presentational components receive plain props and never import the query client, which keeps them testable and reusable.

Trade-offs

Where TanStack Query shines, and where it doesn't

Strengths

  • Removes most hand-written loading, error and cache code from dashboards.
  • Invalidation by query key keeps lists and detail views consistent after mutations.
  • Transport-agnostic: works the same over REST, GraphQL or tRPC.

Limitations

  • Query key design matters; inconsistent keys cause stale views or over-fetching.
  • Not a client-state store; mixing UI state into it creates confusion.

Sources

Official documentation

General descriptions on this page are based on the official documentation linked below. Experience notes are my own.

FAQ

TanStack Query questions

TanStack Query or Redux for a dashboard?

Both, for different jobs. TanStack Query owns data that lives on the server (orders, products, users). Redux Toolkit, or something lighter like Zustand, owns state that only exists in the browser. Putting server data in Redux means re-implementing caching and invalidation by hand.

Does TanStack Query work with Next.js Server Components?

Yes. Data can be prefetched on the server and hydrated into the client cache, so the first render has data and later interactions refetch in the background.

What is a query key in TanStack Query?

A query key is an array that uniquely identifies cached data, for example ['orders', 'list', { status: 'open' }]. TanStack Query caches, deduplicates and refetches by key, and invalidating a key prefix after a mutation refreshes every matching query.

Need a TanStack Query developer?

If your project needs TanStack Query, send a short description of what you're building and where it's stuck. I'll reply with how I'd approach it.