Sub topic 15
Inspecting the cache with the Devtools
This chapter assumes you can set up a QueryClientProvider, write useQuery calls with query keys, and explain staleTime and gcTime. The Devtools let you see those ideas working in a running app. They show every query in the cache, its status, and when it will refetch.
Installing and mounting the Devtools
The Devtools ship as a separate package.
npm install @tanstack/react-query-devtoolsMount the component inside QueryClientProvider, because it reads the client from context.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
const queryClient = new QueryClient()
export function App() {
return (
<QueryClientProvider client={queryClient}>
<Todos />
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
)
}A small floating button appears in a corner of the page. Click it to open the panel. By default the Devtools are included only in development builds, and they are left out of production bundles when your bundler sets process.env.NODE_ENV to production.
Reading the query list
The panel lists one row per cache entry, labeled with its query key. A row shows a count of active observers and a color for its state. Each state follows from concepts you have already met.
| State | Meaning |
|---|---|
| fresh | Data is younger than `staleTime`. The cache serves it without refetching. |
| fetching | A request is in flight right now. |
| stale | Data is older than `staleTime`. It is shown, but a trigger such as a window focus can refetch it. |
| inactive | No mounted component uses the query. The entry stays until `gcTime` expires. |
Select a row to see its details. The details pane shows the current data, the last update time, and the number of observers. Use it to check that ['todos', 1] and ['todos', 2] are separate entries, or that a key you expected to match is actually different.
Actions you can run from the panel
The details pane has buttons that trigger cache operations by hand.
- Refetch runs the query function again.
- Invalidate marks the query stale and refetches it if it is displayed. This is the same effect as
queryClient.invalidateQueries. - Reset returns the query to its initial state.
- Remove deletes the entry from the cache.
- Trigger loading and Trigger error force those states so you can check your loading and error UI without breaking your server.
A debugging routine
When a screen shows stale or wrong data, check the Devtools in this order:
- Find the row. If no row exists for the key, the component is not running the query you think it is.
- Compare keys. A missing parameter in the key causes two screens to share one entry.
- Check the state. A query that stays fresh will not refetch, so lower
staleTimeif that is not what you want. - Press Invalidate and watch whether the UI updates. If it does, the bug is in your
onSuccessinvalidation. If it does not, the bug is in rendering.
Exercise
A todo list does not refresh after a mutation. In the Devtools, the ['todos'] row stays "fresh" and never shows "fetching". Pressing the panel's Invalidate button makes the list update. Where is the likely bug?
Show answer
The mutation never invalidates the ['todos'] key. Manual invalidation works, so the query and rendering are fine. Add queryClient.invalidateQueries({ queryKey: ['todos'] }) to the mutation's onSuccess, and check that the key matches exactly.