Skip to content

Commit 39ed575

Browse files
committed
fix duplicate posts on load
1 parent dcf4312 commit 39ed575

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/components/Feed.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ const Feed: React.FC<FeedProps> = memo(({ subreddit, initialTime, initialSort })
6464
.then((response) => response.json())
6565
.then((data) => {
6666
const fetchedPosts = data.data.children.map((child: { data: Post }) => child.data);
67-
setPosts((prevPosts) => [...prevPosts, ...fetchedPosts]);
67+
setPosts((prevPosts) => {
68+
const existingPostIds = new Set(prevPosts.map((p: Post) => p.id));
69+
const uniqueNewPosts = fetchedPosts.filter((p: Post) => !existingPostIds.has(p.id));
70+
return [...prevPosts, ...uniqueNewPosts];
71+
});
6872
setAfter(data.data.after);
6973
setHasMore(!!data.data.after);
7074
});

0 commit comments

Comments
 (0)