Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TanStack Query throws 'No QueryClient set' on useQuery

ErrorError: No QueryClient set, use QueryClientProvider to set one

Short 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.

  • Applies to React 19, @tanstack/react-query 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1grep finds no QueryClientProvider, or only one in a file that is never importedThere is no QueryClientProvider at allFix
    2. 2The error comes from the same component that returns <QueryClientProvider>The component that calls useQuery renders the provider itselfFix

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 dev

Confirmed by tanstack.com

Did this fix it?

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.tsx

2Fix

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 dev

Confirmed by tanstack.com

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.