Skip to content

Conversation

@camjackson
Copy link
Contributor

@camjackson camjackson commented Dec 31, 2025

🎯 Changes

The docs currently do not specify anything about null or undefined for queryFns. I.e., whether each value is legal, what the semantics are etc. I found a few cases of people being confused about it, including this discussion. That discussion mentioned a change in behaviour which is documented in v3->v4 migration guide, but I think this should be in the main docs as well.

✅ Checklist

  • I have followed the steps in the Contributing guide.
    • I tried to run the docs locally but kept getting errors about DATABASE_URL. I've reviewed the rendered markdown from my changes though and it looks fine to me
  • I have tested this code locally with pnpm run test:pr.
    • N/A, no code changes

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Documentation
    • Clarified guidance: query functions must not return undefined; return null to represent "nothing" as a successful cached value.
    • Added a practical example showing a query function that fetches items by ID and uses a null fallback when an item isn't found.

✏️ Tip: You can customize this high-level summary in your review settings.

@changeset-bot
Copy link

changeset-bot bot commented Dec 31, 2025

⚠️ No Changeset found

Latest commit: 339d39f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR%3A%20undefined%20vs.%20null%20in%20query-functions.md%0A)

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Dec 31, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 31, 2025

📝 Walkthrough

Walkthrough

Documentation change: clarifies that query functions must not return undefined (treated as failed query) and recommends using null to represent a successful "nothing" value; adds an example showing a queryFn that returns item lookup results with null fallback when not found.

Changes

Cohort / File(s) Summary
Documentation Update
docs/framework/react/guides/query-functions.md
Clarified that returning undefined from a queryFn is treated as a failed query and null should be used to represent "no value"; added example demonstrating a query function fetching items by ID and returning null when an item is missing.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 I hopped through code and found the cue,

Undefined fails, but null says true,
A lookup that returns a graceful nil,
Keeps cache calm and helps devs chill,
Hooray for docs — a tidy view!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description covers the motivation and change, but the contribution and testing checklist items were not completed due to local setup issues.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title directly addresses the main documentation change: clarifying the distinction between undefined and null in query-functions.md.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link

nx-cloud bot commented Dec 31, 2025

View your CI Pipeline Execution ↗ for commit 339d39f

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

☁️ Nx Cloud last updated this comment at 2025-12-31 09:36:52 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 31, 2025

More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10008

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10008

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10008

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10008

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@10008

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@10008

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10008

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10008

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@10008

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@10008

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10008

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10008

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@10008

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10008

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10008

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@10008

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10008

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10008

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@10008

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10008

commit: 339d39f

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: 1

🧹 Nitpick comments (1)
docs/framework/react/guides/query-functions.md (1)

8-9: Minor style improvements for conciseness.

Two small refinements from a style perspective:

  1. Line 8: Three successive sentences begin with "If". Consider rewording one to avoid repetition.
  2. Line 9: "All of the following" → "All the following" is more concise.
🔎 Proposed style refinements
-If **resolving the data**, the contained value may be anything **except `undefined`**. If your query function resolves to `undefined`, this will be [treated as a failed query](https://tanstack.com/query/latest/docs/framework/react/guides/migrating-to-react-query-4#undefined-is-an-illegal-cache-value-for-successful-queries). If you need to store "nothing" as a successful result in the query cache, resolve `null` instead.
+**Resolving the data** with `undefined` is not allowed. When a query function resolves to `undefined`, it will be [treated as a failed query](https://tanstack.com/query/latest/docs/framework/react/guides/migrating-to-react-query-4#undefined-is-an-illegal-cache-value-for-successful-queries). To store "nothing" as a successful result in the query cache, resolve `null` instead.

-All of the following are valid query function configurations:
+All the following are valid query function configurations:
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 14c0ec8 and 65ee7db.

📒 Files selected for processing (1)
  • docs/framework/react/guides/query-functions.md
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-11-22T09:06:05.219Z
Learnt from: sukvvon
Repo: TanStack/query PR: 9892
File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335
Timestamp: 2025-11-22T09:06:05.219Z
Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.

Applied to files:

  • docs/framework/react/guides/query-functions.md
📚 Learning: 2025-11-02T22:52:33.071Z
Learnt from: DogPawHat
Repo: TanStack/query PR: 9835
File: packages/query-core/src/__tests__/queryClient.test-d.tsx:242-256
Timestamp: 2025-11-02T22:52:33.071Z
Learning: In the TanStack Query codebase, the new `query` and `infiniteQuery` methods support the `select` option for data transformation, while the legacy `fetchQuery` and `fetchInfiniteQuery` methods do not support `select` and should reject it at the type level.

Applied to files:

  • docs/framework/react/guides/query-functions.md
🪛 LanguageTool
docs/framework/react/guides/query-functions.md

[style] ~8-~8: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...al-cache-value-for-successful-queries). If you need to store "nothing" as a succes...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)


[style] ~9-~9: Consider removing “of” to be more concise
Context: ...e query cache, resolve null instead. All of the following are valid query function conf...

(ALL_OF_THE)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test
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

🧹 Nitpick comments (1)
docs/framework/react/guides/query-functions.md (1)

10-10: Minor: Consider removing "of" for conciseness (optional).

The static analyzer suggests "All the following" instead of "All of the following" for slightly more concise phrasing. Both are valid; this is purely stylistic.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 65ee7db and 8223729.

📒 Files selected for processing (1)
  • docs/framework/react/guides/query-functions.md
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: sukvvon
Repo: TanStack/query PR: 9892
File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335
Timestamp: 2025-11-22T09:06:05.219Z
Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.
Learnt from: DogPawHat
Repo: TanStack/query PR: 9835
File: packages/query-core/src/__tests__/queryClient.test-d.tsx:242-256
Timestamp: 2025-11-02T22:52:33.071Z
Learning: In the TanStack Query codebase, the new `query` and `infiniteQuery` methods support the `select` option for data transformation, while the legacy `fetchQuery` and `fetchInfiniteQuery` methods do not support `select` and should reject it at the type level.
📚 Learning: 2025-11-22T09:06:05.219Z
Learnt from: sukvvon
Repo: TanStack/query PR: 9892
File: packages/solid-query-persist-client/src/__tests__/PersistQueryClientProvider.test.tsx:331-335
Timestamp: 2025-11-22T09:06:05.219Z
Learning: In TanStack/query test files, when a queryFn contains side effects (e.g., setting flags for test verification), prefer async/await syntax for clarity; when there are no side effects, prefer the .then() pattern for conciseness.

Applied to files:

  • docs/framework/react/guides/query-functions.md
📚 Learning: 2025-11-02T22:52:33.071Z
Learnt from: DogPawHat
Repo: TanStack/query PR: 9835
File: packages/query-core/src/__tests__/queryClient.test-d.tsx:242-256
Timestamp: 2025-11-02T22:52:33.071Z
Learning: In the TanStack Query codebase, the new `query` and `infiniteQuery` methods support the `select` option for data transformation, while the legacy `fetchQuery` and `fetchInfiniteQuery` methods do not support `select` and should reject it at the type level.

Applied to files:

  • docs/framework/react/guides/query-functions.md
🪛 LanguageTool
docs/framework/react/guides/query-functions.md

[style] ~9-~9: Consider removing “of” to be more concise
Context: ...e query cache, resolve null instead. All of the following are valid query function conf...

(ALL_OF_THE)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test
🔇 Additional comments (2)
docs/framework/react/guides/query-functions.md (2)

8-8: Clear and helpful guidance on undefined vs. null semantics.

This addition directly addresses the user confusion mentioned in the PR objectives and provides accurate, actionable guidance. The link to the migration guide is a nice touch for users seeking deeper context.


28-34: Well-crafted example demonstrating the null fallback pattern.

This example is practical and directly reinforces the guidance on line 8. The variable naming is consistent (allTodosById used correctly throughout), and the null coalescing operator is an idiomatic way to show the recommended pattern. This is a good real-world use case for returning null to represent a successful "nothing" value.

@camjackson
Copy link
Contributor Author

Failing PR check ends with:

/opt/hostedtoolcache/node/24.8.0/x64/bin/npx size-limit --json
[
  {
    "name": "react full",
    "passed": false,
    "size": 12136,
    "sizeLimit": 12130
  },
  {
    "name": "react minimal",
    "passed": true,
    "size": 9142,
    "sizeLimit": 9150
  }
]
Error creating comment. This can happen for PR's originating from a fork without write permissions.
Error: Size limit has been exceeded.

I do have "Allow edits by maintainers" set on this PR. Not sure what the "size limit" thing is about.

@TkDodo
Copy link
Collaborator

TkDodo commented Dec 31, 2025

the docs sentence is fine but the example makes little sense. Why would we fetch all items and then only access one per todo? This is a situation where you’d want the select option, not do the lookup in the queryFn.

@camjackson camjackson changed the title docsreact-query): undefined vs. null in query-functions.md Dec 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

2 participants