T
TanStack3y ago
typical-coral

Can you pass a QueryClient directly to useQuery instead of using a QueryClientProvider

Is something like the following possible or do you need a global QueryClientProvider?
function myComponent() {
const queryClient =
useRef(
new QueryClient()
).current;

const {data} = useQuery(
['posts'],
fetchPosts,
{ queryClient }
);

// Some component logic
}
function myComponent() {
const queryClient =
useRef(
new QueryClient()
).current;

const {data} = useQuery(
['posts'],
fetchPosts,
{ queryClient }
);

// Some component logic
}
1 Reply
unwilling-turquoise
unwilling-turquoise3y ago
yes, you can in v5 but if you don't use a Provider anywhere, you need to setup calls to queryClient.mount() and unmount() yourself

Did you find this page helpful?