I need to build a system to fetch a unknown number of documents (images, pdfs etc...) to a page.
I have a request that returns to me a list of documents and some metadata, which I then need to fetch so they can be displayed.
I am trying to make a custom hook
to manage concurrent queries so that I can limit the number of queries being fetched simultaneously (for example 3 by 3). Here is a scaffold of the custom hook I am trying to create:
import { useQueries } from "react-query";
function useConcurrentQueries({
queryKey,
queryFunction,
options,
idList,
concurrentQueriesLimit,
}) {
const enabledIds = []
const queries = useQueries(
idList?.length > 0 && idList
.map(id => {
return {
queryKey: [queryKey, id],
queryFn: () => queryFunction(id),
...options,
staleTime: Infinity,
enabled: enabledIds.includes(id)
}})
);
// additional code goes here
}
I would like as much as possible trying not to refetch()
, so that, if later on I am trying to query something that is already in the cache, it wont do so (those documents are not going to change overtime and have a staleTime of Infinity.
Is this the right approach to achieve what I am trying to do ?
How would you guys solve this problem ?
Cheers folks ^^
I tried looking up this problem on Google, using BingAi and reading through the TanStack documentation but I can't seem to find a solution...