reactjs - 如何在 reactjs 中的 Scroll 上从服务器加载更多内容
问题描述
我想调用 API 并使用滚动从数据库加载更多项目,最初,我只想显示五个记录,然后如果用户滚动并到达滚动底部,那么我想再显示五个记录。它即将延迟加载。请我是 reactjs 的新手,我该如何实现它。
这是我的代码。
{this.state.selected ==='text' && <div style={{overflow:'auto', height:'200px'}} data-tab-content="tab2">
{this.state.textList.length>0 ? this.state.textList.map(text=>
<p >{text.text} </p>
):<p>no record found</p>} */}
</div>}
我在这里制作
// get Text List
getTextList(){
debugger
fetch(baseUrl +`/language/listtext/${'1'}/${'5'}/${this.state.lesson_id}/${this.state.premiumprice_id}`)
.then(response => response.json())
.then(data => {
debugger
if(data.status ===200){
debugger
this.setState({
textList: data.text.docs,
})
}else{
this.setState({textList : []})
}
})
}
预先感谢
解决方案
在 div 上添加 onScroll 事件,例如
// for example <div onScroll={this.handleScroll}/>
handleScroll = (e) => {
const bottom = Number((e.target.scrollHeight - e.target.scrollTop).toFixed(0)) - e.target.clientHeight < 50;
let page = this.state.page;
if (bottom) {
// write fetching logic here...
}
};
推荐阅读
- javafx - 将事件处理程序添加到选项卡上下文菜单会引发空指针异常
- javascript - VueJS - 动态过滤 v-for 中的项目
- google-api - 使用 Google Vision API 检测文本会忽略某些字符
- java - Android Studio 中的文件路径
- pyqt5 - 使用搜索栏在列表中查找项目
- google-app-maker - 如何在谷歌应用程序制造商的自动保存模式下实现保存记录的回调功能?
- spring-boot - 是否可以使用 Keycloak 对新用户触发自定义操作?
- sccm - WQL/SCCM - 正确比较版本号(小于...)
- javascript - 如何使用 webpack-4 解析 sass 文件中的背景图像 URL?
- django - 将标头添加到 Django 重定向