useQueries hook for fetching unknown number of entities
See original GitHub issueWhen fetching an unknown amount of entities, the useQueries
hook comes handy, e.g.:
let filteredUsers: number[] = [/* Unknown amount of IDs */];
const results = useQueries(
filteredUsers.map((id) => ["user.find", id] as const);
)
The typings of useQueries
went through significant overhauls lately, which might make it less cumbersome to infer tRPC’s end-to-end types.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:6 (5 by maintainers)
Top Results From Across the Web
useQueries | TanStack Query Docs
The useQueries hook can be used to fetch a variable number of queries: `tsx.
Read more >Missing an equivalent of React Query's useQueries hook #1041
There does not seem to be an equivalent of the React Query's useQueries hook in SWR. Being able to run a variable number...
Read more >Fetching data with queries | Full-Stack Quickstart
We'll use Apollo Client's useQuery React Hook to execute our new query within the Launches component. The hook's result object provides properties that...
Read more >How to fetch n dependent data with react-query - Stack Overflow
How should I do this with react-query useQuery hook? Example 1: const { data} = useQuery("USERS", fetchUsers); data.map ...
Read more >Common data fetching patterns for real apps with react-query
Fetching data: Fetch data and handle results: useQueries . Fetch a subset of data using query strings or query params: useQuery with parameters ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, it’s very hard to deal with the types on this one which is why I haven’t added support for it.
@TkDodo, the react-query maintainer, even brought up on Twitter today that he can barely follow how the types work himself 😅 https://twitter.com/TkDodo/status/1487804403356680201
To me, this feels like an edge-case and I’m unsure if I want to support this as part of the wrapper, to be honest.
However, you can still use
useQueries
and achieve this if you want:Thank you for providing this workaround @KATT. Let me try to convince you that it’s not an edge case.
The big reason for having a native
trpc.useQueries
would be smart cache validation (https://tanstack.com/query/v4/docs/guides/query-invalidation). I would like to query for 10 todos (by id) and then update a single one of them without having to refetch all 10. Ideally, the code would be something like …