Rule

0 installs · 0 said it worked

PatrickJS@patrickjs·
CodingRule· for Cursor

Cursor rules for React SPAs with TanStack Router and Query

Cursor rules that set the assistant up as a React, TanStack Router v1, and TanStack Query v5 expert, covering loader-based prefetching, query option definitions, search-param state, and post-mutation cache updates.

People use this for

  • react tanstack router query setup
  • cursor rules react spa
  • tanstack query loader prefetching
  • react router data loading patterns
  • typescript react cache invalidation mutations
Name
cursor-rules-for-react-spas-with-tanstack-router-and-query
Works with
Cursor
License
CC0(as observed at the source)
Version
v1 · updated Nov 29, 2026
Imported · UnclaimedCC0

Imported on Nov 29, 2026 from github.com . License observed at the source on Nov 29, 2026: CC0.

Are you the author?Claim this profileRequest removal

Use this rule

$ curl -fsSL "https://promptcommunity.dev/api/v1/posts/cursor-rules-for-react-spas-with-tanstack-router-and-query-4ade/export/mdc" -o .cursor/rules/cursor-rules-for-react-spas-with-tanstack-router-and-query.mdc

Then open a new session; Cursor picks it up from .cursor/rules/.

Download cursor-rules-for-react-spas-with-tanstack-router-and-query.mdc
Manual install

Save cursor-rules-for-react-spas-with-tanstack-router-and-query.mdc as .cursor/rules/cursor-rules-for-react-spas-with-tanstack-router-and-query.mdc for this project or ~/.cursor/rules/cursor-rules-for-react-spas-with-tanstack-router-and-query.mdc for every project, then start a new session.

Rule


description: "React SPA with TanStack Router v1 + TanStack Query v5 — the definitive pattern for zero-loading-spinner routing, type-safe URLs, and cache-first data" globs: ["src/routes//*", "src/queries//*", "src/lib/router.ts", "src/lib/queryClient.ts"] alwaysApply: false

You are an expert in React, TanStack Router v1, TanStack Query v5, TypeScript, and Vite.

Architecture

  • TanStack Router: routing, URL state, navigation
  • TanStack Query: server state, caching, mutations
  • Loader = bridge: prefetches into Query cache before render → zero loading spinners for route data
  • Components are pure UI: read from Query cache, trigger mutations

Setup

// src/lib/queryClient.ts
export const queryClient = new QueryClient({
  defaultOptions: { queries: { staleTime: 60_000 } },
})

// src/lib/router.ts
export const router = createRouter({
  routeTree,
  context: { queryClient },
  defaultPreload: 'intent',
  defaultPreloadStaleTime: 0,
})

declare module '@tanstack/react-router' {
  interface Register { router: typeof router }
}

// src/main.tsx
<QueryClientProvider client={queryClient}>
  <RouterProvider router={router} context={{ queryClient }} />
</QueryClientProvider>

Query Definitions

// src/queries/posts.ts
export const postKeys = {
  all: ['posts'] as const,
  detail: (id: string) => [...postKeys.all, 'detail', id] as const,
  list: (f?: PostFilters) => [...postKeys.all, 'list', f] as const,
}

export const postQueryOptions = (id: string) =>
  queryOptions({ queryKey: postKeys.detail(id), queryFn: () => fetchPost(id) })

export const postsQueryOptions = (filters?: PostFilters) =>
  queryOptions({ queryKey: postKeys.list(filters), queryFn: () => fetchPosts(filters) })

Loader + Component (zero loading state)

export const Route = createFileRoute('/posts/$postId')({
  loader: ({ context: { queryClient }, params }) =>
    queryClient.ensureQueryData(postQueryOptions(params.postId)),
  component: PostDetail,
})

function PostDetail() {
  const { postId } = Route.useParams()
  const { data: post } = useQuery(postQueryOptions(postId))  // always in cache from loader
  return <h1>{post!.title}</h1>
}

Search Params → Query Key

const searchSchema = z.object({ page: z.number().default(1), q: z.string().optional() })

export const Route = createFileRoute('/posts/')({
  validateSearch: searchSchema,
  loader: ({ context: { queryClient }, location: { search } }) =>
    queryClient.ensureQueryData(postsQueryOptions(search)),
  component: PostsList,
})

function PostsList() {
  const search = Route.useSearch()
  const { data } = useQuery(postsQueryOptions(search))
  // ...
}

Mutations

const mutation = useMutation({
  mutationFn: createPost,
  onSuccess: (newPost) => {
    queryClient.setQueryData(postKeys.detail(newPost.id), newPost)  // warm cache
    queryClient.invalidateQueries({ queryKey: postKeys.list() })
    navigate({ to: '/posts/$postId', params: { postId: newPost.id } })  // instant — no spinner
  },
})

Hover Prefetching

<Link
  to="/posts/$postId"
  params={{ postId: post.id }}
  onMouseEnter={() => queryClient.prefetchQuery(postQueryOptions(post.id))}
>
  {post.title}
</Link>

Key Rules

  • Always define queryOptions outside components — never inline inside useQuery()
  • Never use useEffect for data fetching — use loaders or useQuery
  • Search params are the single source of truth for filter/pagination state
  • After mutations: setQueryData + invalidateQueries for instant UI feedback
  • declare module '@tanstack/react-router' router registration is required for full type safety

No result recorded yet. Added it? File a Tried-it report so the next person knows how it went in your editor.

Tried it

Reports from people who added this rule to their editor. They roll up into the badges above.

No one has filed a report yet. Try the prompt and be the first.

License: CC0v1 · published Nov 29, 2026

Comments

No comments yet. If you tried this prompt, say how it went.