T
TanStack2y ago
foreign-sapphire

What data strategy should I use?

I have another situation, similar to my last post, but in a different application. The application has more than one List screen, each with it's own paginated list of Item's. The Item in question is the same for each screen, but each screen is querying our backend with different filters. There is also one Details screen that takes in an ID for an Item and displays it. This is my current data strategy: - List Screens - query the backend using keys in the form ["items", "lists", {...filters}, pageNumber] - upon successful query, loops through Item's and uses queryClient.setQueryData to set each Item object as the data for a query with a key in the form ["items", "details", itemId] - Details screen - Takes in an ID for an Item and called useQuery with the key ["items", "details", itemId] and a query function to call the backend This works, but the Details screen seems to immediately call the query function, despite there already being data in the cache for that query key. Why is that? Is there a better way to handle this? Note that only the ID is passed to the Details screen, so context as to what cached list this item could be in is lost when navigating to the Details screen.
2 Replies
inland-turquoise
inland-turquoise2y ago
Have you set staleTime? If not - that's the reason
foreign-sapphire
foreign-sapphireOP2y ago
I'll take a look!

Did you find this page helpful?