State Management · Middle · Question 65 of 75
How do you fetch a dynamic number of queries in parallel (e.g. one per selected id)?
const results = useQueries({
queries: ids.map((id) => ({ queryKey: ["user", id], queryFn: () => getUser(id) })),
});- ✓Correct answer: With
useQueries - 2By calling
useQueryinsideids.map - 3By chaining
thenin a singlequeryFn
Explanation
Calling hooks in a loop breaks the rules of hooks when the length changes. useQueries caches each query separately and has a combine option.