Fetch data only when first call was successful
Hi guys, I created 2 custom hooks to fetch Products and 2nd to fetch Categories, what i wanted to achieve is to fetch categories only when there is products data, below hooks and implementation: And usage on the page: Unfortunately categories always go first... what am I doing wrong here?
4 Replies
automatic-azure•4y ago
Hello! I think you need to set the enabled property on the categories query. Not on the products query
So when the products query is a success then that will enable the categories query to true and then it will fire
extended-salmonOP•4y ago
Thanks, but the tricky part in here is that there is a second place where I am using which is CategoriesSidebar component and this sidebar is always visible (pls view the screenshot) so that makes no matter what you do categories go first...

conscious-sapphire•4y ago
I don't see the problem? You just refactor the categories hook such that it accepts an
enabled parameter just like the products hook.
And then you do something along the lines of:
Then the useShopCategories query will stay idle until products were successfully loaded
Like, why do categories have to go first?
Is it because the two different queries are used in two different components?extended-salmonOP•4y ago
Hi @Evert and thanks for reply, the reason being I will have to make some data modification and for that purpose I need to be sure I have products and categories in one place (cause I need 2 datas), however without products data, would not be possible to make this data manipulation. Also I wanted to teach myself how dependant queries may work