Query data on-demand
In some cases I want to run a couple of queries in parallel on-demand (a button click for example).
I have tested various ways to do this:
- Use mutations with
- Set
- Add a flag for
- Use
I currently (option 4) have created functions in my services like this:
In my component I can then get a couple of promises and call
Like I mentioned above, I still need to manually handle loading states etc. so I feel this is not a TanStack style approach.
What would be the best way to trigger multiple queries in parallel and await them?
I have tested various ways to do this:
- Use mutations with
mutateAsync and Promise.all -> works but does not use caching- Set
enabled: false on the query and use refetch() -> works but does not use caching- Add a flag for
enabled and toggle it to start the queries -> works but hard to await the queries (like await Promise.all)- Use
queryClient.ensureQueryData -> works but you have to manually handle loading states etc.I currently (option 4) have created functions in my services like this:
In my component I can then get a couple of promises and call
await Promise.all to wait for all the queries to complete.Like I mentioned above, I still need to manually handle loading states etc. so I feel this is not a TanStack style approach.
What would be the best way to trigger multiple queries in parallel and await them?