I've been asked this 3 times now and it seems to be a common mistake when following the docs for React Query.
Error: No QueryClient set, use QueryClientProvider to set one
Probably the shortest post I'm going to make but just like Redux, React Query needs to provide the data to the entire app via a provider
import { QueryClientProvider, QueryClient } from "react-query";
const queryClient = new QueryClient();
export default function App({ Component, pageProps }) {
return (
<QueryClientProvider client={queryClient}> // HERE!!!
<Component {...pageProps} />
</QueryClientProvider>
);
}
Then from any component or page within your app, you can pull that data in and display it
import { useQuery } from "react-query";
const { data, refetch } = useQuery("comments", fetchComments);
Here's a link to the getting started guide that might help.
Top comments (4)
I've done it, but some how it keeps throwing the error.
On _app.tsx
on my login.tsx (login page):
Please Help
Heya, why are you initialising queryclient inside a usestate hook? Where did you get that example from?
I tryed initializing the QueryClient outside the component as well, but the problem continue.
In the official guide:
tanstack.com/query/v4/docs/react/g...
it's inicializing in a useState, so i tryed too.
There's a particular situation, if i open the root page(index.tsx) that don't use any useQuery, then navigate to loginPage that there's a useQuery, the problem don't apers, but if i'm on loginPage, e refresh(F5) it throws the error :(
Does the _document.tsx have something todo with it?
The QueryClientProvider is wrapping components which are inside the App(). So useQuery() can be called inside App() children components. I was tried to put useQuery() inside App()...