T
TanStack2y ago
united-yellow

can you use a useQuery as a queryFn?

I am debating between making a new api or just using the cashe of another and filtering the results. Is it possible to use a useQuery within a useQuery?
5 Replies
flat-fuchsia
flat-fuchsia2y ago
You can use queryClient.getQueryData in your queryFn And filter the result
united-yellow
united-yellowOP2y ago
cool thanks
stormy-gold
stormy-gold2y ago
Or use the select property and share the cache ensureQueryData might be even better
flat-fuchsia
flat-fuchsia2y ago
Yeah, probably. I was answering assuming the query wasn't in the same component. Sitting in meetings and bounced around some ideas. Perhaps not even use a second useQuery at all and just setQueryData in the main queryFn and then have const data = queryClient.getQueryData([key, filtered]) wherever it's needed. There's lots of ways to solve this tbh Probably lame, but:
queryKey: [key],
queryFn: async () => {
const data = await getData()

// Store the filtered results too
queryClient.setQueryData([key, 'filtered'], data.filter(...))

return data
}
queryKey: [key],
queryFn: async () => {
const data = await getData()

// Store the filtered results too
queryClient.setQueryData([key, 'filtered'], data.filter(...))

return data
}
united-yellow
united-yellowOP2y ago
neet assumed you needed to use the hook

Did you find this page helpful?