
TanStack React Query v5 is the current major version of the library, and it reshaped the API around clearer names and a single calling style. If you learned React Query in v3 or v4, several familiar options were renamed or removed, while new hooks made suspense and mutation tracking easier.
This article groups the changes by how much they affect everyday code, shows before and after snippets and finishes with a practical migration plan. Exact minor versions change often, so check the npm registry for the latest release number.
Object Syntax Everywhere
Earlier versions accepted several call signatures, such as passing the key and function as separate arguments. Version 5 accepts only a single options object for useQuery, useMutation, useInfiniteQuery and the QueryClient methods. The change improves type inference and makes code read the same everywhere.
// v4
useQuery(['todos'], fetchTodos, { staleTime: 5000 });
// v5
useQuery({ queryKey: ['todos'], queryFn: fetchTodos, staleTime: 5000 });Renamed Options and States
Two renames touch almost every codebase. The cacheTime option became gcTime, which better describes garbage collection of inactive data. The loading status became pending, so isLoading was replaced by isPending for the case where no data exists yet.
| v4 name | v5 name | Notes |
|---|---|---|
| cacheTime | gcTime | Same behavior, clearer name |
| status: 'loading' | status: 'pending' | No cached data yet |
| isLoading | isPending | isLoading now means pending and fetching |
| isInitialLoading | isLoading | Previous name deprecated |
| useErrorBoundary | throwOnError | Same purpose |
| keepPreviousData: true | placeholderData: keepPreviousData | Helper function import |
Why isLoading Still Exists
In v5, isLoading is true only when the query has no data and a request is actually running. A disabled query with no data is pending but not loading. Use isPending for most loading screens and isLoading when you need to know a fetch is in progress.
Callbacks Removed From useQuery
The onSuccess, onError and onSettled callbacks were removed from queries because they ran once per observer and caused confusing side effects. Mutations keep their callbacks. For queries, derive values during render, use effects for genuine side effects or handle errors globally through the QueryCache.
const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error) => toast.error(error.message),
}),
});New Suspense Hooks
Suspense moved from a boolean option to dedicated hooks: useSuspenseQuery, useSuspenseInfiniteQuery and useSuspenseQueries. With these hooks, data is always defined in TypeScript because loading and error states are handled by Suspense and error boundaries instead of flags.
Infinite Query Changes
Infinite queries now require an initialPageParam instead of relying on an undefined default. A new maxPages option limits how many pages stay in the cache, which helps long feeds stay memory friendly and refetch faster.
useInfiniteQuery({
queryKey: ['feed'],
queryFn: ({ pageParam }) => fetchFeed(pageParam),
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
maxPages: 5,
});Simpler Optimistic Updates
Version 5 lets you render optimistic values directly from mutation variables. While a mutation is pending, show its variables in the list, then let invalidation replace them with server data. The new useMutationState hook reads pending mutations from anywhere in the tree.
- No manual cache writes are needed for simple optimistic lists.
- Rollback happens naturally because the variables disappear on error.
- The older cache-based approach still works for complex cases.
Environment Requirements
The minimum React version rose to 18 because the library now relies on useSyncExternalStore. TypeScript and browser targets were also modernized, dropping support for some older environments. Projects that cannot move to React 18 should stay on v4 until they upgrade.
Package Names Stay the Same
Version 5 continues to use @tanstack/react-query and @tanstack/react-query-devtools. Only version 3 used the old react-query name, so v4 projects simply bump version numbers. The installation article lists the commands.
A Practical Migration Plan
Upgrading works best in small, verifiable steps. Most of the mechanical changes can be automated, leaving the behavioral changes for careful review.
- Upgrade React to version 18 or later first, and confirm tests pass.
- Bump @tanstack/react-query and the DevTools package together.
- Run the codemod published by TanStack to convert call signatures.
- Rename cacheTime, isLoading and useErrorBoundary where the codemod did not.
- Replace query callbacks with render logic, effects or QueryCache handlers.
- Add initialPageParam to every infinite query.
Testing After the Upgrade
Watch loading states closely, since the isPending and isLoading split changes when spinners appear. Open the DevTools and confirm keys, freshness and garbage collection behave as before.
Should You Upgrade Now?
New projects should start on v5, because examples, documentation and community answers target it. Existing v4 apps gain clearer types and new hooks, but the move can wait until a planned maintenance window if the current code is stable.
Changes for TypeScript Users
Version 5 improves type inference across hooks, especially when queryOptions helpers share configuration between components and prefetch calls. The default error type also became Error instead of unknown, which removes many manual type casts in error handling code.
The queryOptions function lets you define a key, function and options once, then pass the result to useQuery, prefetchQuery or setQueryData with full type safety. It replaces many hand-written key factories and keeps related settings together.
export const todosQuery = queryOptions({
queryKey: ['todos'],
queryFn: fetchTodos,
staleTime: 30_000,
});
useQuery(todosQuery);
queryClient.prefetchQuery(todosQuery);Combining Parallel Queries
The useQueries hook gained a combine option that merges the results of many queries into a single value. Instead of mapping over an array of results in every component, you can return exactly the shape the interface needs, such as a list of data plus one pending flag.
Common Upgrade Pitfalls
Loading spinners that never disappear usually come from disabled queries checking isLoading where isPending was intended, or the reverse. Missing initialPageParam values cause type errors in infinite queries, and removed query callbacks silently stop running if the codemod could not convert them.
Review every place that relied on onSuccess for side effects, such as resetting forms or showing toasts. Moving that logic into mutation callbacks or global cache handlers keeps the behavior explicit and avoids running it once per observing component.
Keeping Dependencies Aligned
Update the DevTools, persistence packages and ESLint plugin to versions that match the new core release. Mixing major versions across related TanStack packages is a frequent source of confusing runtime errors.
Conclusion
TanStack React Query v5 trades a few renames for a more consistent and better typed API. The object syntax, gcTime, isPending and dedicated suspense hooks are the changes you will notice first.
Plan the upgrade with the steps above, then revisit the caching lifecycle and the React Query tutorial to see the new names in context.



