tanstack-query

A set of coding rules for TanStack Query, a library that loads, caches, refreshes, and updates data from web services in React applications.

In plain words
What is it for?
Use it when defining cache keys, data-fetching functions, refresh rules, create/update/delete operations, and temporary interface updates before a server response.
Why use it?
It prevents stale or mismatched cached data and keeps data loading, errors, updates, and optimistic changes consistent.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/tanstack-query
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection
Per session 695 This file is loaded in full into every session.
When invoked 695 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00695 $0.00695
Opus 5 $0.00347 $0.00347
Sonnet 5 $0.00139 $0.00139
Haiku 4.5 $0.00069 $0.00069

Measured 2d ago against content hash ca65ccc88cd4, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

tanstack-query scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules-mdc/frameworks/tanstack-query.mdc · 60 lines

How it starts

The opening of the file, as written. The whole thing — 60 lines — stays where its author put it; the contents beside it link to each section on GitHub.

TanStack Query Cursor Rules

You are an expert in TanStack Query (React Query v5). Follow these rules:

Query Keys

  • Use query key factories: export const userKeys = { all: ['users'], detail: (id: string) => ['users', id] }
  • Never use inline string arrays — always reference the factory
  • Include all variables that affect the response in the key
  • Structure keys hierarchically for targeted invalidation

Query Functions

  • Define queryFn as a separate named function, not inline
  • Always type the return value of queryFn explicitly
  • Throw errors in queryFn — don't return { error } objects. TanStack handles errors via the error state
  • Use the queryKey passed to queryFn via the context parameter for DRY key usage

Query Options

  • Set staleTime based on data volatility: user profile (5min), dashboard stats (30s), real-time feed (0)
  • Use placeholderData instead of initialData when the placeholder is approximate
  • Set gcTime (garbage collection) higher than staleTime — default 5min is usually fine
  • Enable refetchOnWindowFocus only for data that changes frequently

Mutations

  • Always use useMutation — never call fetch directly in event handlers
  • Set onSuccess to invalidate related queries: queryClient.invalidateQueries({ queryKey: userKeys.all })
  • Return the promise from mutateAsync when you need to await the result
  • Use mutate (not mutateAsync) in fire-and-forget scenarios like button clicks

Optimistic Updates

  • Use the onMutate → onError → onSettled pattern for optimistic updates
  • In onMutate: cancel outgoing queries, snapshot previous data, set optimistic data
  • In onError: roll back to the snapshot from onMutate context
  • In onSettled: always invalidate to refetch the true server state

Error & Loading States

  • Check isPending (not isLoading) in v5 — isLoading is isPending && isFetching
  • Always handle the error state — don't just show loading forever
  • Use isPlaceholderData to show a visual indicator for stale placeholder data
  • Wrap query-heavy pages in an ErrorBoundary with QueryErrorResetBoundary

Read the full file on GitHub · 60 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 60 lines · 695 tokens per session scan A ca65ccc88cd4

Subscribe to this mod's changes

tanstack-query is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 695 tokens to every session, about $0.0035 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.