Fullstack CourseLearn by building
Back to week 2

Topic

RTK Query vs TanStack Query (and when to use which)

Definition

RTK Query is Redux Toolkit’s optional data-fetching and caching addon (createApi / fetchBaseQuery). TanStack Query is a dedicated server-state library. Both can cache remote data; this monorepo standardizes Nest HTTP on TanStack Query + Axios while RTK core holds client drafts.

In simpler words

RTK can fetch data too (RTK Query). We teach it so you recognize it — but Nest HTTP lists in this course use TanStack Query.

Cover the RTK Query APIs from Getting Started, then the ownership rule for this course.

After this you can

  • Name createApi, fetchBaseQuery, ApiProvider, setupListeners
  • Sketch a tiny createApi endpoint
  • Explain why this repo uses TanStack Query for Nest
  • Keep RTK slices for client-only state alongside either library

What RTK Query is

From the official docs: RTK Query lives inside @reduxjs/toolkit as a separate entry point. It removes hand-written fetch/cache logic and stores its cache in Redux.

Import createApi from @reduxjs/toolkit/query/react to get auto-generated React hooks.

Docs rule of thumb: one API slice per base URL.

Minimal createApi shape

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const api = createApi({
  reducerPath: "api",
  baseQuery: fetchBaseQuery({ baseUrl: "/api" }),
  endpoints: (build) => ({
    getTickets: build.query<Ticket[], void>({
      query: () => "/tickets",
    }),
    createTicket: build.mutation<Ticket, NewTicket>({
      query: (body) => ({ url: "/tickets", method: "POST", body }),
    }),
  }),
});

export const { useGetTicketsQuery, useCreateTicketMutation } = api;
// Also add api.reducer + api.middleware to configureStore

Valid RTK Query. apps/web Nest path uses Axios + TanStack Query — know both.

What this monorepo chooses

TanStack Query + Axios withCredentials for Nest HTTP lists.

RTK createSlice for client drafts that are not server truth yet.

Do not run RTK Query and TanStack Query for the same Nest list.

Ownership table

Client draft text     → RTK createSlice
Nest list / detail    → TanStack Query (+ Axios)
Nest auth cookie      → browser httpOnly + Nest guards
Other codebases       → often RTK Query createApi

One server-cache owner per resource.

Mistake: two caches for the same list

// Wrong
useGetNotesQuery();
useQuery({ queryKey: ["notes"], queryFn: list });

// Right here
useNotesQuery(); // TanStack only

Duplicate caches desync after writes.

Interview one-liners

“RTK is modern Redux: configureStore, createSlice, thunks, entity adapters, selectors.”

“RTK Query is the fetching addon in the same package; TanStack Query is a separate server-state library.”

“On this project: RTK for client drafts, TanStack Query for Nest HTTP.”

One-liner

RTK core = client shared state
RTK Query = optional Redux-powered server cache
TanStack Query = our Nest server cache

Clear beats clever.

Live playground

Ownership picker

Choose draft vs Nest list — see which tool fits.

TanStack Query — server-owned cache + invalidate after mutations.

Keep in mind

  • RTK Query Quick Start via Tutorials Overview: https://redux-toolkit.js.org/tutorials/overview
  • Learn createApi so foreign codebases make sense.
  • Ship Nest features here with TanStack Query + RTK drafts (Week 4 Notes projects).

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. What is RTK Query?
Syntax2. Which import gives React hooks for RTK Query endpoints?
Practical3. Official rule of thumb for createApi?
Practical4. What does fetchBaseQuery wrap?
Logic5. Why does this monorepo use TanStack Query for Nest tickets?
Logic6. What goes wrong if RTK Query and TanStack Query both own the same Nest list?
Concept7. What is setupListeners for in RTK Query?
Syntax8. Where do client wizard drafts belong here?
Practical9. ApiProvider is useful when…
Concept10. Best interview one-liner for this repo?