T
TanStack15mo ago
correct-apricot

useInfiniteQuery cache for useQuery

Best explained by example website.com/todos/ Show all todos with useInfiniteQuery , results are stored in cache
const queryKey = ['todos']
const queryKey = ['todos']
website.com/todos/{todoId} Show a specific todo with useQuery
const queryKey = ['todo', todoId]
const queryKey = ['todo', todoId]
Since the keys are different (as infinite query stores a page of data), how might we be able to update the cache manually for the single todo cache? It seems silly to fetch a list of 10 items and then additionally fetch the same 10 items individually as they are clicked on when we already have them.
2 Replies
foreign-sapphire
foreign-sapphire15mo ago
What’s more expensive? Calling a simple API or manipulating the cache? (: Don‘t optimize a thing that does not need optimization
correct-apricot
correct-apricotOP15mo ago
Fair point, but in this case its a good optimization. On the mobile app if you are on the todos screen and load 10 todos, then click on one it takes you to that screen with that todo. If would have to hit the api again and take time to load. I guess pre-fetch is an option but still it's not ideal. So it's both a cost to actual price since charged per read, and then the cost to performance since you are double fetching the same data

Did you find this page helpful?