Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openapi-react-query] Uncaught Error: No QueryClient set, use QueryClientProvider to set one #1947

Open
1 task done
bogoblin opened this issue Oct 11, 2024 · 9 comments
Open
1 task done
Assignees
Labels
docs Requires an update to docs openapi-react-query Relevant to openapi-react-query

Comments

@bogoblin
Copy link

Description

I'm trying out openapi-react-query and I don't seem to be able to get it to work. I'm creating a client like so:

const fetchClient = createFetchClient<paths>({
    baseUrl: "http://localhost:8880"
});
export const $api = createClient(fetchClient);

...and then using it in a component like so:

    const { data, isLoading } = $api.useQuery("get", "/query", {
        params: {
            query: {
                player: false,
                playlists: false,
                playlistItems: true,
                plref: playlistId,
                plrange: '0:100000',
                // @ts-expect-error LibraryColumns is readonly
                plcolumns: LibraryColumns
            }
        }
    });

However, when I run this in the browser, I get the following error message:

openapi-react-query.js?v=2cd51558:985 Uncaught Error: No QueryClient set, use QueryClientProvider to set one
    at useQueryClient (openapi-react-query.js?v=2cd51558:985:11)
    at useBaseQuery (openapi-react-query.js?v=2cd51558:1080:18)
    at useQuery (openapi-react-query.js?v=2cd51558:1151:10)
    at Object.useQuery (openapi-react-query.js?v=2cd51558:1234:66)
    at FoobarLibrary (foobarLibrary.tsx:143:38)
    at renderWithHooks (react-dom_client.js?v=a0b8382c:11548:26)
    at mountIndeterminateComponent (react-dom_client.js?v=a0b8382c:14926:21)
    at beginWork (react-dom_client.js?v=a0b8382c:15914:22)
    at HTMLUnknownElement.callCallback2 (react-dom_client.js?v=a0b8382c:3674:22)
    at Object.invokeGuardedCallbackDev (react-dom_client.js?v=a0b8382c:3699:24)

I've been reading online about react-query and it seems you need to add a QueryClientProvider around your app, but I'm not sure how to do that.

Proposal

If someone can help me fix this I will contribute to the docs and add a more helpful error message so that others don't have this issue.

Checklist

@bogoblin bogoblin added enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library labels Oct 11, 2024
@bogoblin bogoblin changed the title Uncaught Error: No QueryClient set, use QueryClientProvider to set one [openapi-react-query] Uncaught Error: No QueryClient set, use QueryClientProvider to set one Oct 11, 2024
@bogoblin
Copy link
Author

Update: I figured it out. I'm going to make a PR to improve the docs.

@riya-amemiya
Copy link

Hello @bogoblin

How did you manage to resolve this error?
I've been struggling with the same issue and haven't been able to find a solution even after searching on Google. Could you please share your solution? It would be really helpful if you could explain the steps you took to fix this problem.

@bogoblin
Copy link
Author

d

Hello @bogoblin

How did you manage to resolve this error? I've been struggling with the same issue and haven't been able to find a solution even after searching on Google. Could you please share your solution? It would be really helpful if you could explain the steps you took to fix this problem.

In the root of your app, you need to create a queryClient with react-query. My root looks like this:

import {StrictMode} from 'react'
import {createRoot} from 'react-dom/client'
import './index.css'
import {FoobarLibrary} from "./foobarLibrary.tsx";
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
import {PlaybackControls} from "./playbackControls.tsx";

const queryClient = new QueryClient({
    defaultOptions: {
        queries: {
            retry: false
        }
    }
});

createRoot(document.getElementById('root')!).render(
    <StrictMode>
        <QueryClientProvider client={queryClient}>
            <FoobarLibrary playlistId="p2"/>
            <PlaybackControls />
        </QueryClientProvider>
    </StrictMode>,
)

@riya-amemiya
Copy link

Thank you so much for the solution! This is incredibly helpful.

@kerwanp
Copy link
Contributor

kerwanp commented Oct 15, 2024

This is a requirement as per the @tanstacl/react-query library and not related to openapi-react-query itself but as this requirement seems to impact a lot of people I'm happy to accept a pull-request that demo how to add the Provider with some links to the official documentation (https://tanstack.com/query/latest/docs/framework/react/quick-start)

@kerwanp kerwanp added docs Requires an update to docs openapi-react-query Relevant to openapi-react-query and removed enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library labels Oct 15, 2024
@Bjorn-Eric-Abr
Copy link

Just to clarify for the documentation - does this only occour when the client is defined in another file than where we use the query?

@kerwanp
Copy link
Contributor

kerwanp commented Nov 6, 2024

Just to clarify for the documentation - does this only occour when the client is defined in another file than where we use the query?

No this will always happen if you dont explicitly provide a QueryClient or implicitly using a QueryClientProvider. https://tanstack.com/query/latest/docs/framework/react/quick-start

@burnto
Copy link

burnto commented Nov 25, 2024

I have a QueryClientProvider and can use standard tanstack-query useQuery hooks fine, but am still seeing this error with openapi-react-query.

@aretel
Copy link

aretel commented Dec 11, 2024

This is a requirement as per the @tanstacl/react-query library and not related to openapi-react-query itself but as this requirement seems to impact a lot of people I'm happy to accept a pull-request that demo how to add the Provider with some links to the official documentation (https://tanstack.com/query/latest/docs/framework/react/quick-start)

The docs covers setting up 'openapi-react-query,' so this information should be included as part of the complete setup guide. Without it, users may encounter difficulties, as seen in this instance (me included)

@gzm0 gzm0 assigned kerwanp and unassigned gzm0 Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Requires an update to docs openapi-react-query Relevant to openapi-react-query
Projects
None yet
Development

No branches or pull requests

7 participants