0

I am having trouble with the useQueries functionality of Tanstack Query (formerly known as React Query).

This example from the documentation (using some example data: let users = [{ id: 1 }, { id: 2 }, { id: 3 }];)

const userQueries = useQueries({
  queries: users.map((user) => {
    return {
      queryKey: ['user', user.id],
      queryFn: () => fetchUserById(user.id),
    }
  }),
});

renders a type error:

Argument of type '{ queries: { queryKey: (string | number)[]; queryFn: () => any; }[]; }' is not assignable to parameter of type 'readonly any[]'.

Object literal may only specify known properties, and 'queries' does not exist in type 'readonly any[]'.ts(2345)

Is the documentation wrong or what am I doing wrong?

Hedvig
  • 41
  • 5
  • What version of React Query are you using? – Jakub Kotrs Apr 19 '23 at 09:47
  • I'm using version 3.39.3. – Hedvig Apr 19 '23 at 10:01
  • 1
    Ok, in that case. You're looking on documentation for v4, the v3 is here https://tanstack.com/query/v3/docs/react/reference/useQueries. The API is different. – Jakub Kotrs Apr 19 '23 at 10:21
  • that's the right answer. Here's a TS playground for v4 showing that it works: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1WO1OAXzgDMoIQ4ByAARgEMA7VRgYwGsB6KdBtmAFoAjjigBPagCgpAG3Tw0uQgF44AbSTAAJgC44ARmIAaRHB36ATCbMW4AZmIBdANwzt6NrIa8yyJgLAEExkCmwAFgCqGFAAQuIAktoAFHZMyCAARrgAlPoAChQgwBgAPFp6cOlZuKZMDCDo+qx4TADmxAB8MmzBrCgx2Lj4qgNYYiPJCFJwcKLDBPpKUKgAdCAMYMnJyzlwKp2IM7NwvDDIUCHTJyfzEgDS6OL66tTL1KbLqzpOxsc3d3EADEmPpknsDqEYBForh4kkdjFvtocn8bkRjkRUVIsW4pMshngCFIuFw4AA9AD8QA – TkDodo Apr 21 '23 at 16:46

0 Answers0