Rule
0 installs · 0 said it worked
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 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.mdcThen open a new session; Cursor picks it up from .cursor/rules/.
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
queryOptionsoutside components — never inline insideuseQuery() - Never use
useEffectfor data fetching — use loaders oruseQuery - Search params are the single source of truth for filter/pagination state
- After mutations:
setQueryData+invalidateQueriesfor 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.
Related rules
- Cursor rules for type-safe React routing with TanStack Router and Query
Cursor rules guiding an AI assistant to build React single-page apps with TanStack Router for routing and TanStack Query for server state, using loaders to prefetch data and avoid loading spinners.
Coding · @patrickjs
- Cursor rules for writing TanStack Query v5 data-fetching code in React
Cursor rules that make the assistant an expert in TanStack Query v5 and TypeScript, covering query keys, queryOptions, mutations, optimistic updates, infinite queries, Suspense, prefetching, and error handling.
Coding · @patrickjs
- Cursor rules for building type-safe TanStack Router apps in React
Cursor rules that guide the AI to write TanStack Router v1 code in React and TypeScript, covering file-based routing, typed params, Zod search validation, loaders, auth guards, and preloading.
Coding · @patrickjs
- Cursor rules for building Next.js App Router apps with TanStack Query v5
Cursor rules guiding the assistant on Next.js App Router data flow with TanStack Query v5: server prefetch with HydrationBoundary, Server Actions as mutations, optimistic updates, infinite queries, and when to use Server Components versus client queries.
Coding · @patrickjs
- Cursor rules for type-safe TanStack Router v1 React routing
Cursor rule set guiding an assistant to build type-safe React routing with TanStack Router v1, covering file-based routes, loaders, Zod-validated search params, auth guards, and TanStack Query integration.
Coding · @patrickjs
License: CC0v1 · published Nov 29, 2026
Comments
No comments yet. If you tried this prompt, say how it went.