TanStackT
TanStack4y ago
4 replies
progressive-amaranth

How can I `useQueryClient` outside a Svelte component?

I'm using the excellent svelte-query – thanks so much Lachlan!

I'd like to abstract my query functions and define them in .ts files away from my .svelte files.

As part of this, I want to use onMutate and onSuccess functions to do optimistic updates etc, e.g.:
onSettled: () => {
    const queryClient = useQueryClient();
    queryClient.invalidateQueries(['lobby', gameID]);
},


The problem seems to be with calling useQueryClient() outside of a Svelte component. I get this error:
Error: Function called outside component initialization
mutation.ts:250 Error: Function called outside component initialization
    at get_current_component (index.mjs:984:15)
    at getContext (index.mjs:1076:12)
    at getQueryClientContext (context.js?v=3319898c:5:20)
    at useQueryClient (useQueryClient.js?v=3319898c:3:25)


Is there any better way to access the queryClient inside my .ts files or inside my query's callback functions?
Was this page helpful?