Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

docs: clarify default behavior of invalidateQueries refetching #9541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Yashkanekar wants to merge 2 commits into TanStack:main
base: main
Choose a base branch
Loading
from Yashkanekar:fix-docs-react-query-invalidateQueries

Conversation

Copy link

@Yashkanekar Yashkanekar commented Aug 7, 2025

This PR updates the documentation for the invalidateQueries method in React Query to clarify the default behavior of query invalidation and refetching.

-It explains that by default, only active queries are immediately refetched.
-Inactive queries are marked stale but not automatically refetched until they become active again.
-It adds instructions on how to force invalidation and refetching of all queries (both active and inactive) by using the type: 'all' or refetchType: 'all' option.

closes #9531

...ching
Update documentation to explain that by default invalidateQueries only refetches active queries,
while inactive queries are marked stale but not immediately refetched. Add guidance on using
type/refetchType 'all' option to refetch all queries (active + inactive) to avoid confusion.
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Aug 7, 2025
Comment on lines 23 to 27
- It is marked as stale. This stale state overrides any `staleTime` configurations being used in `useQuery` or related hooks
- If the query is currently being rendered via `useQuery` or related hooks, it will also be refetched in the background

> **Important:** By default, `invalidateQueries` will immediately refetch *only active queries*—those currently used by mounted components. Inactive queries (cached but not in use) are marked as stale but will not be refetched until they become active again (e.g., when a component utilizing the query mounts). This means that calling `invalidateQueries` without additional options does *not* guarantee all matching queries are refetched immediately.

Copy link
Collaborator

Choose a reason for hiding this comment

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

this is pretty much the same as the two points above are also saying. The second one only says "is currently being rendered via useQuery or related hooks", maybe this could be re-worded to "active queries", and then explained what an active query is.

@@ -131,3 +134,20 @@ const todoListQuery = useQuery({
```

[//]: # 'Example5'

By default, `invalidateQueries` only refetches active queries. If you want to refetch **all matching queries**, including those currently inactive in the cache, you need to explicitly set the `refetchType` (or `type`) option to `'all'`:
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is partially wrong. setting type: 'all' wouldn't achieve that. It's also the default behaviour.


This forces every matching query—active or inactive—to be refetched immediately.

> 🧠 **Note:** The default behavior (`refetchType: 'active'`) helps avoid unnecessary network requests. But if you're triggering an invalidation after a mutation that affects *all clients or views*, make sure to use `refetchType: 'all'` to keep all query caches up to date.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don’t understand that sentence - what is "all clients or views" referring to? Imo there’s really no need to set refetchType: 'all', as it will likely lead to overfetching. It's something we have documented in the API docs, but I don’t think we need to emphasize on this with a guide.

Copy link

nx-cloud bot commented Aug 7, 2025
edited
Loading

View your CI Pipeline Execution ↗ for commit 4ee626c

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

☁️ Nx Cloud last updated this comment at 2025年08月07日 15:46:13 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@TkDodo TkDodo TkDodo left review comments

Assignees
No one assigned
Labels
documentation Improvements or additions to documentation
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Documentation mismatch: invalidateQueries default behavior claims "all" but only refetches "active" queries

AltStyle によって変換されたページ (->オリジナル) /