我正在创建一款社交媒体应用程序,允许用户互相关注。我意识到它现在正在返回数千条被关注的人的帖子。我只想返回最后十篇帖子。我假设有某种过滤方法只允许渲染最后 10 个帖子(不进行切片)。
useEffect(() => {
if (props.following.length !== 0) {
const feed = [...props.feed]
.filter(({ user }) => props.following?.includes(user?.uid))
.sort((x, y) => y.creation - x.creation);
setPosts(feed);
}
}, [JSON.stringify(props.following), JSON.stringify(props.feed)]);