Multiple call with react-query
Hi guys, i am relatively new to RG and i need advise in following example: Let's asume I have Products page where i am using hook for fetching products data, on the same page (route) i would like to fetch all categories - different enpoint, so it looks like i need to do 2 different calls on the same page? Is this something like Promise.all? What would be the best approach? Thank you
4 Replies
wise-white•4y ago
have a look at: https://tanstack.com/query/v4/docs/guides/dependent-queries
Dependent Queries | TanStack Query Docs
Dependent (or serial) queries depend on previous ones to finish before they can execute. To achieve this, it's as easy as using the enabled option to tell a query when it is ready to run:
`tsx
genetic-orangeOP•4y ago
Thank you, so u would recommend to fetch categories if products.length > 0 for example ?
wise-white•4y ago
yes, for example. or, if you need one query for each product, useQueries can help
genetic-orangeOP•4y ago
Thank you for previous replies @TkDodo 🔮 I meant however a bit different situation: Let's say I fetch list of categories i will display in Sidebar and list of products i will fetch on Store Page, I need however both datas (categories and products) in the same place - basically i need to manipulate the data - in redux for example i would take categories from store using redux hook, how can i handle such an example? Please bare in mind both I mean Products page and Sidebar are 'far away' from each other