T
TanStack•3y ago
fair-rose

useInfiniteQuery save objects individually

I am using useInfiniteQuery to implement a flatlist with posts of my app. Each page of useInfiniteQuery fetch 15 posts. Its is possible to save each post individually in a query like
useInfiniteQuery( queryKey: ['posts'], queryFn: () => arrayofPosts)
useInfiniteQuery( queryKey: ['posts'], queryFn: () => arrayofPosts)
I want to use this hook in another screen:
useQuery( queryKey: ['post', postId], queryFn: () => postData)
useQuery( queryKey: ['post', postId], queryFn: () => postData)
I want to use the data of each post individually and avoid fetching it again in another screen because i have this data inside useInfiniteQuery Thanks
5 Replies
foreign-sapphire
foreign-sapphire•3y ago
Hi 👋 I think you could leverage a staleTime and the select option in a custom hook to achieve a similar API
fair-rose
fair-roseOP•3y ago
Sory buy i dont understand your solution
sensitive-blue
sensitive-blue•3y ago
If the data already exists in the useInfiniteQuery, you could create a custom hook usePost(postId) that would call useInfiniteQuery internally and then find the post you're looking for in the returned data (data will be returned from the cache). To avoid having to repeat useInfiniteQuery with the same key and query function, you could also wrap that in a custom hook usePosts(). Then usePost() would call usePosts() and look for the post id in the results.
foreign-sapphire
foreign-sapphire•3y ago
As above. Thanks for elaborating Julien 🙂
fair-rose
fair-roseOP•3y ago
Thanks you so much both of you

Did you find this page helpful?