Skip to content

Conversation

coryhouse
Copy link
Contributor

@coryhouse coryhouse commented Aug 23, 2025

Summary by CodeRabbit

  • Documentation
    • Updated the React external data loading guide to include a concrete QueryClient instance in the TanStack Query example.
    • Ensures the loader and suspense query usage reference a real client and shared cache in the snippet.
    • Improves clarity and correctness of the example without altering any APIs or behavior.
    • No other content changes; examples remain functionally consistent with prior guidance.

Copy link
Contributor

coderabbitai bot commented Aug 23, 2025

Walkthrough

Adds a QueryClient instantiation to a TanStack Query example in a React Router docs page so the loader and hooks reference a concrete client instance. No other code or APIs are changed.

Changes

Cohort / File(s) Summary
Docs: React Router external data loading example
docs/router/framework/react/guide/external-data-loading.md
Inserted const queryClient = new QueryClient() before the postsQueryOptions definition to clarify the client used by loader: () => queryClient.ensureQueryData(...) and cache-driven hooks. No other edits.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User as User
  participant Router as Router Loader
  participant QC as QueryClient
  participant API as Posts API
  participant UI as React Component

  User->>Router: Navigate to /posts
  Router->>QC: ensureQueryData(postsQueryOptions)
  alt Cache miss
    QC->>API: fetch posts
    API-->>QC: posts data
    QC-->>Router: cache filled
  else Cache hit
    QC-->>Router: return cached data
  end
  Router-->>UI: render route
  UI->>QC: useSuspenseQuery(postsQueryOptions)
  QC-->>UI: serve cached posts
Loading

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

I twitched my nose at snippets old,
Popped in a client, crisp and bold.
Loader hops, the cache now sings,
Queries glide on tiny wings.
Burrow built, no fuss, no fright—
Data fetched, then nibbled right. 🥕

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the documentation Everything documentation related label Aug 23, 2025
Copy link

nx-cloud bot commented Aug 23, 2025

View your CI Pipeline Execution ↗ for commit 4ea18c1

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 2s View ↗

☁️ Nx Cloud last updated this comment at 2025-08-23 00:02:14 UTC

Copy link

pkg-pr-new bot commented Aug 23, 2025

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@5016

@tanstack/directive-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/directive-functions-plugin@5016

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@5016

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@5016

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@5016

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@5016

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@5016

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@5016

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@5016

@tanstack/react-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-plugin@5016

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@5016

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@5016

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@5016

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@5016

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@5016

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@5016

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@5016

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@5016

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@5016

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@5016

@tanstack/server-functions-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/server-functions-plugin@5016

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@5016

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@5016

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@5016

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@5016

@tanstack/solid-start-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-plugin@5016

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@5016

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@5016

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@5016

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@5016

@tanstack/start-server-functions-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-client@5016

@tanstack/start-server-functions-fetcher

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-fetcher@5016

@tanstack/start-server-functions-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-functions-server@5016

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@5016

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@5016

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@5016

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@5016

commit: 4ea18c1

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/router/framework/react/guide/external-data-loading.md (1)

109-136: Broken snippet: queryClient is undefined in this second example.

The “Error handling” example calls queryClient.ensureQueryData(...) but never defines or retrieves queryClient in this snippet. Readers copying this block will hit a ReferenceError.

Prefer pulling the client from the router context (consistent with the SSR section below) instead of creating a new singleton here:

 export const Route = createFileRoute('/')({
-  loader: () => queryClient.ensureQueryData(postsQueryOptions),
+  loader: ({ context: { queryClient } }) =>
+    queryClient.ensureQueryData(postsQueryOptions),
   errorComponent: ({ error, reset }) => {

If you want the bare-minimal fix (client-only), alternatively add a local instance above the loader:

 export const Route = createFileRoute('/')({
+  // For client-only examples; see SSR section below for per-request setup
+  const queryClient = new QueryClient()
   loader: () => queryClient.ensureQueryData(postsQueryOptions),

(First option recommended for consistency across docs.)

🧹 Nitpick comments (3)
docs/router/framework/react/guide/external-data-loading.md (3)

78-79: Clarify provider context (or show it) to avoid runtime errors with useSuspenseQuery.

useSuspenseQuery requires a QueryClientProvider higher in the tree. Consider adding a brief comment or a one-line provider example link here to make it clear that the app must be wrapped, otherwise this snippet will throw at runtime.

You can inline a minimal hint without cluttering the snippet:

-const queryClient = new QueryClient()
+// Ensure your app is wrapped in <QueryClientProvider> at the root.
+const queryClient = new QueryClient()

86-93: Verify returned data shape for posts.

The component destructures data: { posts }, which implies fetchPosts() returns { posts: Post[] }. Many examples return Post[] directly. If fetchPosts() returns an array, this destructure will throw. Either adjust the destructure or clarify the expected return shape in the snippet/comment.

Possible change if fetchPosts() returns an array:

-const {
-  data: { posts },
-} = useSuspenseQuery(postsQueryOptions)
+const { data: posts } = useSuspenseQuery(postsQueryOptions)

78-88: Optional: avoid module-scoped singletons to stay SSR-safe and consistent with the SSR section.

Since the “SSR Dehydration/Hydration” section correctly shows per-request QueryClient creation and passing via router context, you can mirror that pattern here too. This avoids teaching a pattern that can leak state across requests.

Example adjustment:

-export const Route = createFileRoute('/posts')({
-  // Use the `loader` option to ensure that the data is loaded
-  loader: () => queryClient.ensureQueryData(postsQueryOptions),
+export const Route = createFileRoute('/posts')({
+  // Use the `loader` option to ensure that the data is loaded
+  loader: ({ context: { queryClient } }) =>
+    queryClient.ensureQueryData(postsQueryOptions),

And you can add a short note above that the QueryClient is provided via router context (as demonstrated in the SSR section below).

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 3f05c0b and 4ea18c1.

📒 Files selected for processing (1)
  • docs/router/framework/react/guide/external-data-loading.md (1 hunks)
🔇 Additional comments (1)
docs/router/framework/react/guide/external-data-loading.md (1)

78-79: Good fix: define a concrete QueryClient for the loader to use.

This resolves the undefined queryClient in the first example’s loader and aligns the cache preloading with TanStack Query’s API.

@@ -75,6 +75,8 @@ Let's take a look at a more realistic example using TanStack Query.

```tsx
// src/routes/posts.tsx
const queryClient = new QueryClient()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you wouldnt create the queryClient instance in a specific route

@coryhouse
Copy link
Contributor Author

Yes, good point. I opened this because I was unclear how to access the queryClient in this location. Is the recommendation to export the queryClient when it's instantiated so it can be accessed elsewhere in spots where useQueryClient can't be called (because a hook isn't valid outside a React component)?

@schiller-manuel
Copy link
Contributor

typically you would add the queryClient to the router context, see for example here https://github.com/TanStack/router/blob/main/examples/react/basic-react-query-file-based/src/main.tsx#L14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Everything documentation related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants