T
TanStack2y ago
blank-aquamarine

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
passive-yellow
passive-yellow2y ago
You can use queryClient.getQueryData in your queryFn And filter the result
blank-aquamarine
blank-aquamarineOP2y ago
cool thanks
magic-amber
magic-amber2y ago
Or use the select property and share the cache ensureQueryData might be even better
passive-yellow
passive-yellow2y 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
}
blank-aquamarine
blank-aquamarineOP2y ago
neet assumed you needed to use the hook

Did you find this page helpful?