react-query-api-integration

A set of rules for using React Query, a library for fetching and updating server data in React applications. It requires React Query for all server-state operations instead of manually combining useEffect and useState.

In plain words
What is it for?
Use it when adding API data fetching or server-side changes to a React application. It covers query hooks, loading and error states, caching, retries, and mutation hooks.
Why use it?
It avoids repeated API-loading code and helps prevent related state-management bugs.

Cursor rule for Cursor

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/rm2thaddeus/pixel_detective/react-query-api-integration
Clone the repo
git clone --depth 1 https://github.com/rm2thaddeus/Pixel_Detective

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 1,362 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. 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.00000 $0.01362
Opus 5 $0.00000 $0.00681
Sonnet 5 $0.00000 $0.00272
Haiku 4.5 $0.00000 $0.00136

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

Security

Grade A, and why

react-query-api-integration scanned grade A with 1 finding 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.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

const api = axios.create({
frontend/.cursor/rules/react-query-api-integration.mdc · 206 lines

How it starts

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

React Query & API Integration Patterns

🎯 MANDATORY: Use React Query for ALL Server State

Sprint 10 revealed that manual useEffect + useState patterns for API calls create unnecessary complexity and bugs. ALWAYS use React Query.

✅ CORRECT PATTERNS:

1. Query Hook Pattern
// ✅ Use useQuery for data fetching
function useCollections() {
  return useQuery({
    queryKey: ['collections'],
    queryFn: async () => {
      const response = await api.get('/api/v1/collections');
      return response.data;
    },
    staleTime: 5 * 60 * 1000, // 5 minutes
    retry: 3,
  });
}

// ✅ Component usage
function CollectionsList() {
  const { data: collections, isLoading, error } = useCollections();
  
  if (isLoading) return <Spinner />;
  if (error) return <ErrorAlert message={error.message} />;
  
  return <CollectionGrid collections={collections} />;
}
2. Mutation Pattern
// ✅ Use useMutation for server modifications
function useCreateCollection() {
  const queryClient = useQueryClient();
  
  return useMutation({
    mutationFn: async (data: CreateCollectionRequest) => {
      const response = await api.post('/api/v1/collections', data);
      return response.data;
    },
    onSuccess: () => {
      // Invalidate and refetch collections
      queryClient.invalidateQueries({ queryKey: ['collections'] });
      toast.success('Collection created successfully');
    },
    onError: (error) => {
      toast.error(`Failed to create collection: ${error.message}`);
    },
  });
}
3. Background Polling Pattern
// ✅ Job status polling with automatic cleanup
function useJobStatus(jobId: string | null) {
  return useQuery({
    queryKey: ['job-status', jobId],
    queryFn: async () => {
      if (!jobId) throw new Error('No job ID provided');
      const response = await api.get(`/api/v1/ingest/status/${jobId}`);
      return response.data;
    },
    enabled: !!jobId, // Only run when jobId exists
    refetchInterval: (data) => {
      // Stop polling when job is complete
      if (data?.status === 'completed' || data?.status === 'failed') {
        return false;
      }
      return 2000; // Poll every 2 seconds
    },
    retry: false, // Don't retry failed polls
  });
}

Read the full file on GitHub · 206 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 · 206 lines · 0 tokens per session scan A 085cde1facbc

Subscribe to this mod's changes

react-query-api-integration is a cursor rule published in the GitHub repository rm2thaddeus/Pixel_Detective (21 stars, last pushed 6mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,362 tokens. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.