TanStack Query throws 'No QueryClient set' on useQuery
Error: No QueryClient set, use QueryClientProvider to set oneShort answer
useQuery reads the QueryClient from React context. The error means no QueryClientProvider is above the component that calls it. Create one QueryClient outside any component, pass it to QueryClientProvider near the root, and make sure the component using the hook renders inside it, not next to it.
Fix it fast
1Find out which cause you have
Find where QueryClientProvider is rendered and where the failing component is rendered. The provider has to be an ancestor of the component in the tree.
grep -rn 'QueryClientProvider' src/2Match what you see, then jump to its fix
What does the error look like?
Error: No QueryClient set, use QueryClientProvider to set one
Cause 1 of 2
There is no QueryClientProvider at all
1Check
Search the entry file (main.tsx, index.js, _app or layout) for the provider.
grep -rn 'new QueryClient' src/2Fix
Create the client once at module level and wrap the app with the provider.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
const queryClient = new QueryClient()
function App() {
return (
<QueryClientProvider client={queryClient}>
<Todos />
</QueryClientProvider>
)
}3Verify
Reload the page. The component fetches and the error is gone from the console.
npm run devConfirmed by tanstack.com
Cause 2 of 2
The component that calls useQuery renders the provider itself
1Check
A component cannot read a provider that it creates in its own return value. Context is read from components above it.
grep -n -E 'useQuery|QueryClientProvider' src/App.tsx2Fix
Move the useQuery call into a child component that the provider wraps.
function App() {
return (
<QueryClientProvider client={queryClient}>
<Todos /> {/* useQuery lives in Todos */}
</QueryClientProvider>
)
}
function Todos() {
const { data } = useQuery({ queryKey: ['todos'], queryFn: getTodos })
return <List items={data} />
}3Verify
The child renders its data. No second error appears.
npm run devConfirmed by tanstack.com
How do I stop it coming back?
- Keep one QueryClient in a module and import it where tests and the app root need it.
- In tests, render the component inside its own QueryClientProvider.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TanStack Query says the query function returned undefined
Query data cannot be undefined. Please make sure to return a value other than undefined from your query function
TanStack Query treats a query that resolves to undefined as failed.
2 causes, each with a fixWeb apps
invalidateQueries or refetch does nothing in React Query and React Native
data does not update after mutation
invalidateQueries only refetches queries whose key starts with the key you pass and that are in use.
4 causes, each with a fixWeb apps
onSuccess and onError on useQuery do nothing after upgrading TanStack Query
onError callback not working after upgrading to React Query v5
TanStack Query v5 removed onSuccess, onError and onSettled from queries, so options passed to useQuery are ignored.
2 causes, each with a fixWeb apps
TanStack Query useInfiniteQuery reloads every page again and again
the network tab shows page 1, 2, 3 requested again one after another
An infinite query is one cached entry with many pages.
3 causes, each with a fixWeb apps
useQuery returns data as undefined
TypeError: Cannot read properties of undefined (reading 'map')
data is undefined until the query succeeds, so check isPending and isError first.
3 causes, each with a fixWeb apps
TypeScript cannot find the module @tanstack/react-query
Cannot find module '@tanstack/react-query' or its corresponding type declarations.
Either the package is not installed under this exact name, or the editor cannot see it.
2 causes, each with a fix