在react中引入下拉刷新和上拉加载

1. 首先引入插件 import ReactPullLoad, {STATS} from 'react-pullload'

2. 初始化:

       constructor(props) {
           super(props);
        this.state = {
            action: STATS.init,
            hasMore: true,
            isMore:1,
            page:1
            
        }

3. // 用来触发加载和刷新的函数

handleAction = (action) => {
    console.info(action, this.state.action, action === this.state.action);
    //new action must do not equel to old action
    if(action === this.state.action) {
        return false
    }

    if(action === STATS.refreshing) { //刷新
        this.handRefreshing();

    } else if(action === STATS.loading) { //加载更多
        this.handLoadMore();
    } else {
        //DO NOT modify below code
        this.setState({
            action: action
        })
    }
}
//刷新
handRefreshing = () => {
    if(STATS.refreshing === this.state.action) {
        return false
    }
    setTimeout(() => {
        //refreshing complete
        this.setState({
            hasMore: true,
            action: STATS.refreshed
        });
    }, 2000)
    this.setState({
        action: STATS.refreshing
    })
}
//加载更多
handLoadMore = () => {
    if(STATS.loading === this.state.action) {
        return false
    }
    setTimeout(() => {
        if(this.state.isMore === 0) {
            this.setState({
                action: STATS.reset,
                hasMore: false
            });
        } else {
            var n=this.state.page;
                n++;
                $.ajax({
                    type: "POST",
                    url:httphead+"/author/goods/getList",
                    data:{
                        page:n,
                        end:10,
                        cateIds:catAllId,
                        keyword:this.state.value
                    },
                    headers:{
                        Authorization:headers_vendor
                    },
                    success:function(data){
                        
                        if(data.code == 100){
                            var nData = this.state.goodList.concat(data.data.goodsList);
                            this.setState({
                                goodList:nData,
                                action: STATS.reset,
                                isMore:data.data.isMore,
                                page:n
                            })
                        }else if(data.code == 500){
                            window.location.href = "/";
                        }else{
                            alert(data.message);
                        }
                    }.bind(this)
                })
        }
    }, 500)

    this.setState({
        action: STATS.loading
    })
}

转载于:https://www.cnblogs.com/panax/p/8510863.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值