reactjs - 如何使用 react-scroll-component 进行分页?
问题描述
我不太明白如何使用这个包,我假设每次滚动到底部时它都会获取有限数量的数据,所以在我的后端我只返回十五个结果但我认为必须有一个分页让 api知道它会返回哪 15 个结果吗?并且我假设的分页将在这个包的变量中,但我没有看到任何内容。谁能告诉我这是如何工作的?这是我的代码
前端
<InfiniteScroll
dataLength={posts.length} //This is important field to render the next data
next={fetchPosts}
hasMore={true}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: 'center' }}>
<b>Yay! You have seen it all</b>
</p>
}
>
{posts.map((p)=>(
//console.log(p.id)
<Post key={p._id} post={p}/>
))}
</InfiniteScroll>
const fetchPosts = async ()=>{
const res = username
? await axios.get("/api/posts/profile/"+ username)
: await axios.get("/api/posts/timeline/all")
let data = ""
data = res.data.data
setPosts(data.sort((p1,p2)=>{
return new Date(p2.createdAt)-new Date(p1.createdAt);
}));
};
后端
public async timelinePost(user_id:string){
const currentUser:any = await LocalAuthModel.findById(user_id)
const friendPosts = await PostModel.find({userId:{$in: [user_id,...currentUser.followings]}}).sort([['createdAt', -1]]).limit(15)
return friendPosts;
}
解决方案
推荐阅读
- windows - 带有 NoBuffering 的 Haskell 跨平台 getChar
- azure - 在 Azure cosmosDB 帐户中禁用低于 1.2 的 TLS
- azure-devops - Azure DevOps - 具有多个看板的多个项目与具有多个存储库的单个项目
- java - Javafx:将 ImageView 绑定到 Anchorpane 或 VBox 并通过调整场景大小来缩放 ImageView
- r - 有效地为每一行在不同的 DataFrame 中找到值
- reactjs - 如何为 Facebook 和 Google 登录创建 AWS Cognito 用户池
- angular - 我的应用程序可以使用 Selenium 自动化吗?
- python - Python:如何使用猴子补丁避免递归
- python - 如何使用 dcc.RadioItems 更新 Dash 中的多个 Plotly 图表?
- sql-server - 尝试删除以前用于数据库触发器的表