react滚动条下拉加载更多

本文探讨如何在React应用中实现滚动条下拉加载更多数据的功能,通过监听滚动事件并结合JavaScript进行判断,实现在内容区域接近底部时自动加载新数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

export default class Home extends Component {
    constructor(props) {
        super(props);
        this.wrapper = React.createRef();
        }
async getOldMore(){
	/*放你的请求地址*/
}
componentDidMount() {
        let timeCount;
        const that=this;

        function callback() {
            const top =that.wrapper.current.getBoundingClientRect().top;
            const windowHeight = window.screen.height;

            if (top && top < windowHeight) {
                // 当 wrapper 已经被滚动到页面可视范围之内触发
                that.getOldMore();
            }
        }

        this.content.current.addEventListener('scroll', function () {
            if (this.state.pagefinished) {
                return ;
            }

            if (timeCount) {
                clearTimeout(timeCount);
            }

            timeCount = setTimeout(callback, 50);
        }.bind(this), false);
  render() {
  	/*你之前的数据*/
	<div className="loadMore" ref={this.wrapper} onClick={()=>this.getOldMore()}>加载更多</div>
	}
}

研究研究就能用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值