reactjs - 如何使用 react-infinite-scrollbar 进行分页
问题描述
我正在使用一个名为 react-infinite-scroller 的库进行分页。
但我没有得到正确的结果。API 仅被调用一次(最初)。我希望当我向下滚动时,它应该再次使用其他记录调用该 API。
这是代码
import InfiniteScroll from 'react-infinite-scroller';
const People = (props) => {
constructor(props) {
super(props);
this.state = {
activeTab: 1,
searchString: '',
data: [],
hasMore: false,
loading: false,
page: 0,
};
}
getPeopleInfo = () => {
this.setState({ loading: true, data: [] });
const { params } = this.props || {};
const { page, searchString } = this.state;
if (params?.params) {
this.props
.getPeople(params.params, page, 100, searchString)
.then((resp) => {
if (resp && resp.data) {
this.setState({
loading: false,
data: this.state.data.concat(resp?.data),
page: page + 1,
hasMore: resp?.recordsTotal !== this.state.data.length,
});
}
})
.catch(() => {});
}
};
fetchMore = () => {
const { activeTab } = this.state;
if (activeTab === 1) {
this.getPeopleInfo();
} else {
this.getConnectedPeople();
}
};
return (
<InfiniteScroll
getScrollParent={myElementRef}
hasMore={hasMore}
useWindow={false}
loadMore={() => this.fetchMore()}
>
{map(this.state.data,(people) => (
<PeopleInfo data={people} />
),
)};
</InfiniteScroll>
)
}
解决方案
推荐阅读
- sql - SQL Server:将 FLOAT 转换为 NVARCHAR,精度最高,无需科学记数法
- javascript - 对象数组中的对象数组
- apache-spark - 如何使用 Spark-submit 命令获取提交到 Spark 集群的作业的应用程序 ID/作业 ID?
- django - 在 docker 中安装哨兵时没有名为“raven”的模块
- java - 通用 BiDiMap
- browser - 如何使 jar 文件在浏览器中作为 java 小程序运行
- swift - 展开 node.name.last() 时发现 nil
- python - 如何从 Python Dataframe 中的多个列中挑选出所有非 NULL 值
- ios - 安装 iPhone 配置实用程序时出错
- angular - 如何更改 Angular 2+ 剑道迷你图的条形颜色