What changed in TanStack Query v5
TanStack Query (still 'React Query' to most of us) reached version 5 in October.
TanStack Query (still “React Query” to most of us) reached version 5 in October. Most of the changes are renames and removals, which sounds dull until one of them quietly changes how your loading states behave. Here are the ones that matter in practice, in the order you’ll probably hit them.
One way to call everything
v4 let you call useQuery with positional arguments or with an object. v5 only accepts the object:
// v4
useQuery(['todos', id], () => fetchTodo(id), { staleTime: 60_000 });
// v5
useQuery({
queryKey: ['todos', id],
queryFn: () => fetchTodo(id),
staleTime: 60_000,
});
The old form doesn’t silently misbehave. It throws, with a message that starts “Bad argument type. Starting with v5, only the ‘Object’ form is allowed”. The same applies to useMutation, invalidateQueries, prefetchQuery and the rest.
There’s a codemod for this one. In 5.13 it ships inside the package:
npx jscodeshift ./src \
--extensions=ts,tsx --parser=tsx \
--transform=./node_modules/@tanstack/react-query/build/codemods/src/v5/remove-overloads/remove-overloads.js
isPending is not the new isLoading
This is the change that causes real bugs. The loading status was renamed to pending, and isLoading was renamed to isPending. But isLoading still exists, with a different meaning: it’s now isPending && isFetching, true only while the first fetch is actually running.
The difference shows up with disabled queries. Here’s a search box that only queries when there’s text:
const search = useQuery({
queryKey: ['search', term],
queryFn: () => searchProducts(term),
enabled: term !== '',
});
I logged the flags for an empty and a non-empty term:
| status | isPending | isLoading | fetchStatus | |
|---|---|---|---|---|
| empty term (disabled) | pending | true | false | idle |
| typing, first fetch | pending | true | true | fetching |
| results arrived | success | false | false | idle |
A disabled query with no data is pending, and stays that way. So a mechanical rename of if (isLoading) to if (isPending) turns this into a spinner that spins forever on an empty search box. For “show a spinner while the first request is running”, isLoading is still the right flag. Use isPending when you mean “there’s no data yet, for whatever reason”.
gcTime, not cacheTime
cacheTime is now gcTime. It was always a confusing name: it’s not how long data counts as fresh (that’s staleTime), it’s how long an unused query stays in memory before it’s garbage collected. The default is still five minutes.
Keeping the previous page on screen
keepPreviousData: true is gone. Its replacement uses placeholderData, with a helper the library exports:
import { useQuery, keepPreviousData } from '@tanstack/react-query';
const { data, isPlaceholderData } = useQuery({
queryKey: ['products', page],
queryFn: () => fetchProducts(page),
placeholderData: keepPreviousData,
});
Moving from page 1 to page 2, data keeps showing page 1 with isPlaceholderData: true until page 2 arrives. That flag is what you use to dim the list or disable the “next” button.
No more onSuccess on queries
onSuccess, onError and onSettled were removed from useQuery. (They’re still on useMutation.) Here’s the uncomfortable part: if you’re in plain JavaScript, passing onSuccess to useQuery doesn’t throw. It’s just ignored. I checked: the callback never ran. TypeScript will catch it, but JavaScript code will quietly lose whatever that callback did.
The callbacks were removed because they ran once per component using the query, not once per fetch, and they encouraged syncing server data into local state. What to use instead depends on what the callback did:
- Showing an error toast: do it once, globally, on the QueryCache.
- Deriving something from the data: compute it during render, or use select.
- Really reacting to new data: a useEffect on data.
import { QueryClient, QueryCache } from '@tanstack/react-query';
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error, query) => {
if (query.state.data !== undefined) {
toast.error(`Couldn't refresh: ${error.message}`);
}
},
}),
});
That version only shows a toast when a background refresh fails, since a first-load failure is better shown in place.
Infinite queries need a starting page
useInfiniteQuery now requires initialPageParam instead of reading a default from your query function:
useInfiniteQuery({
queryKey: ['feed'],
queryFn: ({ pageParam }) => fetchFeed(pageParam),
initialPageParam: 1,
getNextPageParam: (lastPage) => lastPage.nextPage,
});
The page params are also kept in the cache now (data.pageParams), and a new maxPages option caps how many pages stay in memory for long feeds.
Smaller renames
- useErrorBoundary is now throwOnError.
is now . - Suspense has dedicated hooks: useSuspenseQuery and friends, where data is never undefined.
- React 18 is the minimum.
How I’d upgrade
Run the codemod, fix the TypeScript errors, and then search the codebase for isLoading, onSuccess, onError and onSettled by hand. The first gives you stuck spinners if renamed carelessly, and the others disappear silently in JavaScript. Those four searches are where the risk is. Everything else fails loudly.