How to make dependent infinite queries? #2740
-
I'm trying to build a forum topic page with infinite scroll, where data are from two sources. The page needs to fetch the My first thought is to write two However, this solution doesn't feel right. Could someone please give me some advice? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Seems to have found a solution that fits the specific requirements of mine, though I'm afraid it might not be the optimal one. const userInfo: UseInfiniteQueryResult<IUser[]> = useInfiniteQuery(
["user", topicId],
({ pageParam = data?.pages[0].map((post) => post.userId) }) =>
ky
.get(
`${API_ROOT}/user?${pageParam
.filter(Boolean)
.map((userId: number) => `id=${userId}`)
.join("&")}`
)
.json(),
{
enabled: open && (data?.pages.length ?? 0) >= 1,
}
);
const [sentryRef] = useInfiniteScroll({
// ...
onLoadMore: () => {
if (isLoading) {
return;
}
fetchNextPage().then((posts) =>
userInfo.fetchNextPage({
pageParam: posts.data?.pages
.flat()
.map((post) => post.userId)
.filter(
(id) =>
!userInfo.data?.pages
.flat()
.map((user) => user.id)
.includes(id)
),
})
);
},
// ...
});
return (
// ...
{data?.pages.map((page, i) => (
// eslint-disable-next-line react/no-array-index-key
<React.Fragment key={i}>
{page.map((post) => (
<div key={post.id}>
<Reply
post={post}
user={userInfo.data?.pages
.flat()
.find((user) => user.id === post.userId)}
/>
</div>
))}
</React.Fragment>
))}
// ...
) |
Beta Was this translation helpful? Give feedback.
Seems to have found a solution that fits the specific requirements of mine, though I'm afraid it might not be the optimal one.