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) })),
});

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.

Further reading

Problem with a question

State Management · Middle · question 65

Test yourself

Take the State Management · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz