react 上拉加载

开发者福利!热门AI工具限时免费用 购周边即赠Coding Plan Lite,Claude Code、Cursor等20+工具畅享,效率翻倍! 阅读详情

今天项目有个上拉加载的需求,ui框架用的是antd-mobile,

然而框架提供的上拉加载,看不懂,

很想搬砖,

但是怕在这块耗得时间过长,所以就自己造了个上拉加载轮子

需要的可以参考下

import React, { Component } from 'react';
import './myFeedBack.less';
import Header from '@/components/header/Header';
export default class Demo extends Component {
  constructor(props) {
    super(props);
    this.state = ({
      feedBackList: [],
      current: 1,
      pageSize: 10,
      pages: 0,
      isLoadingMore: false
    });
  }
  componentWillMount() {
    this.getList()
  }
  render() {
    return (
      <div className={this.state.isLoadingMore ? "container haveBg" : "pageContxt haveBg"}>
        <Header history={this.props.history} backIcon title="我的反馈" />
      <div className='myFeedBackPage'>
        <ul className="feedBackList">
          {
            this.state.feedBackList.map((item,index) => {
              return (
                <li className='feedBackItem' key={index}>
                  <div className="feedBackAsk">
                    <img src={require("img/myFeedBack/ask.png")} alt="" />
                    <p>{item.opinion}</p>
                    <span>{item.createTime}</span>
                  </div>
                  <div className="feedBackAnswer">
                    <img src={require("img/myFeedBack/answer.png")} alt="" />
                    <p>{item.feedback}</p>
                    <span>{item.confirmTime}</span>
                  </div>
                </li>
              )
            })
          }
        </ul>
        <div className="loadMore" ref="wrapper" onClick={this.loadMoreDataFn.bind(this, this)}>
          {
            this.state.isLoadingMore ?  '暂无数据' : <p><img src={require("img/myFeedBack/loading.gif")} alt="" /></p>
          }
        </div>
      </div>
      </div>
    );
  }
  getList = () => {
    const url = '接口'
    window.util.post(url,{
      current: this.state.current,
      pageSize: this.state.pageSize,
      userId: localStorage.getItem('userId')
    }).then((res) => {
      console.log(res);
      this.setState({
        pages: res.page.pages
      })
      
      if (res.data.list.length === 0) {
        this.setState({
          isLoadingMore: true
        })
      }else {
        if (res.data.list.length === parseInt(this.state.pageSize, 10)) {
          this.setState({
            isLoadingMore: false,
            feedBackList: this.state.feedBackList.concat(res.data.list)
          })
        }else {
          this.setState({
            isLoadingMore: true
          })
          this.setState({
            feedBackList: this.state.feedBackList.concat(res.data.list)
          })
        }
      }
    }).catch(e => console.log(e))
  }
  componentDidMount() {
    const wrapper = this.refs.wrapper;
    const loadMoreDataFn = this.loadMoreDataFn;
    const that = this; // 为解决不同context的问题
    let timeCount;
    function callback() {
      const top = wrapper.getBoundingClientRect().top;
      const windowHeight = window.screen.height;
 
      if (top && top < windowHeight) {
        // 已经被滚动到页面可视范围之内触发
        loadMoreDataFn(that)
      }
    }
 
    window.addEventListener('scroll', function () {
        if (this.state.isLoadingMore) {
          return ;
        }
        if (timeCount) {
          clearTimeout(timeCount);
        }
        timeCount = setTimeout(callback, 50);
    }.bind(this), false);
  }
 
  loadMoreDataFn(that){
    const url = '接口'
    let currentTemp = ++that.state.current
    window.util.post(url,{
      current: currentTemp,
      pageSize: that.state.pageSize,
      userId: localStorage.getItem('userId')
    }).then((res) => {
      if(currentTemp < that.state.pages+1 ) {
        that.setState({
          feedBackList: that.state.feedBackList.concat(res.data.list)
        })
      }
      if(currentTemp > that.state.pages) {
        that.setState({
          isLoadingMore: true
        })
      }
    })
    
  }
}

 

React-实现上拉加载更多 1. 写在前面我最开始纠结当用户滑动时onTouchMove事件会不停的执行去调接口,于是我侥幸的想只用onTouchEnd事件去判 断用户是否滑到最底部,但是这种方法应用到项目中才发现点击的时候也会触发onTouchEnd,实际应用并不理想。 光判断滑到最底部是不够的,首先需要知道用户现在的操作,是点击还是滑动(向上、向下、向左、向右),这里 受到了[原生js判断手指滑动方向][1]的启发。 2. 阅读详情

相关推荐

react-native实现上拉刷新下拉加载

每次加载十条数据,将加载好的数据放在缓存中 import React, { Component } from 'react'; import { Platform, StyleSheet, Text, View, Image, TextInput, Dimensions, FlatList, NativeModules, ...

shizhihua11的博客 1066

react中实现上拉加载数据功能

react中实现上拉加载数据功能

qq_42618175的博客 1809

react native实现上拉加载下拉刷新

前言我们在做原生app开发的时候,很多场景都会用到下拉刷新、上拉加载的操作,Android中如PullToRefreshListView,ios中如MJRefresh等都是比较好用,且实现上比较简单的第三方库。他们的实现原理大体相同,都是在列表的基础上新增头部和尾部,然后新增手势触摸的逻辑判断。那么对于react native,我们也可以用相同的原理来实现。react-native-pull这里我们

xiangzhihong8的专栏 9989

react中的上拉加载

6.样式最外层所有高度设置100%, con属性加绝对定位 over-flow-y:scroll。5.在最外层的盒子加ref与onScroll属性。3. 使用useEffect监听滚动条。4.组件中使用antd中的组件。2.获取数据时切割数据。7.回到顶部的蒙版层样式。

weixin_63813990的博客 625

react中的,(原生js和组件)上拉刷新,触底(下拉)刷新,回到顶部

由于在某些浏览器下(例如 Chrome),当窗口内嵌页面高度小于显示器高度时,`window.screenTop` 的值会比实际值偏小(通常比 `window.screenY` 小一点),因此习惯上一般使用 `window.innerHeight+window.screenY>=window.screenTop-1` 或者 `window.innerHeight+window.screenY>=document.body.offsetHeight-1` 来判断浏览器是否滚动到页面底部。

kkkys_kkk的博客 1793

React上拉加载(React-PullLoad)

1、下载react-pullload npm ireact-pullload 2、在组件中去引用 import ReactPullLoad,{ STATS } from "react-pullload"; 3、css样式 ①引用插件内的样式 import "node_modules/react-pullload/dist/ReactPullLoad.css"; ②或者直接引入使用下列代码: .pull-load { ...

BAIZHUKE的博客 1627

reactreact移动端不规则瀑布流布局上拉加载更多

reactreact移动端-函数组件-瀑布流布局-上拉加载更多

东小小川 1287

react上拉加载更多

react 前言 最近在写的项目中有上拉加载更多的需求。抽空就来写一篇文章。 上拉加载更多,下拉刷新,在原生 App 上经常都用到,既符合用户的使用习惯,也有很多成熟的库可以直接拿来使用。那么在WebApp中怎么实现呢?今天就我们就探讨一下。一、思路 上拉刷新的可以通过判断某个点到窗口顶部的距离的值与浏览器窗口高度的值的大小来实现。假如说标识点在 WebApp 的底

inyiyi的博客 9669

React上拉加载和下拉刷新

最近在做一个功能,就是上拉加载下一页,用的是react搭建前端视图,以下是我的做法和遇到的相关问题及解决办法: 案例一:回到顶部 class Home extends Component { consrcutor(props) { super(props); this.state={ showScroll: false } } component...

CamilleZJ的博客 6671

react native学习笔记13——FlatList上拉加载

我们可以利用官方组件RefreshControl实现下拉刷新功能,但React Native官方没有提供相应的上拉加载的组件,因此在RN中实现上拉加载比下拉刷新要复杂一点。 虽然没有直接提供上拉加载的组件,不过我们仍可以通过FlatList的onEndReached与onEndReachedThreshold属性来实现相应效果。ActivityIndicator这里上拉加载的转圈效果用Activi

MrOnion的专栏 1万+

关于react-native中FlatList的 上拉加载更多,下拉刷新

前言:在react-native项目中,列表是非常常见的,并且react-native官方也有提供列表组件FlatList;但是这个组件本身的上拉加载更多,下拉刷新属性是有一定问题的,需要我们字段去控制才能完美的实现。 代码实现: ==>> 主要是实现 上拉加载更多,下拉刷新 功能,可能存在一些其他的自定义组件没有引入 1. 首先,定义一个FlatList列表 <Fla...

halo1416的博客 2324

React Native 实现FlatList的下拉刷新上拉加载

实现的功能:     1、下拉刷新,使用原生下拉头。     2、上拉加载,自定义加载布局。     3、同时也可以添加底部布局。     4、是否显示空白布局。 FlatList的封装 /** * noEmptyRemind 是否...

mamr227的博客 1804

React-Native使用FlatList组件实现上拉加载功能

写作时间:2020/4/17 React-Native版本:0.62 本案例实现了FlatList组件的上拉加载功能,数据源于知乎,URI有失效可能,但整个代码结构可以参考 import React, {Component} from 'react'; import {ActivityIndicator, StyleSheet, FlatList, View, Text, Alert} from ...

weixin_42405831的博客 630

react 上拉刷新下拉加载

react 上拉刷新下拉加载https://www.cnblogs.com/qq120848369/p/5920420.htmlhttp://blog.csdn.net/sinat_17775997/article/details/64127482

前端kk的博客 2721

React Native使用FlatList组件实现上拉加载

运行成功截图: 代码如下: import React, { Component } from 'react'; import { View, StyleSheet, FlatList, Text, RefreshControl, ActivityIndicator, } from 'react-native'; import { thisExpression } from '@babel/types'; const tempDatas = [ { color:'bl

weixin_44824839的博客 535

react移动端上拉加载更多组件

在开发移动端react项目中,遇到了上拉加载更多数据的分页功能,自己封装了一个组件,供大家参考,写的不好还请多多指教!   import React, {Component} from 'react'; import cssModuleHandler from "../../../utils/cssModuleHandler"; import styleObject from './Lo...

weixin_30404405的博客 882

react-native 给ScrollView添加上拉加载和下拉刷新

目录上拉加载下拉刷新 上拉加载 添加onScrollEndDrag事件 <ScrollView onScrollEndDrag={this.onMomentumScrollEnd} > ... </ScrollView> onScrollEndDrag事件方法 属性值计算,offSetY + oriageScrollHeight >= contentSizeHeight - 1可判断下拉到底部 我加了pageLoadingFull属性判断是

在下月亮有何贵干 2634

react-native FlatList 上拉刷新 下载加载更多

import React,{Component} from 'react'; import {View,Text, Image, FlatList,RefreshControl} from 'react-native'; export default class HomeScreen extends Component { static navigationOptions = {...

dengye7077的博客 248

React中实现图片预加载、延迟加载上拉加载、下拉更新

1.jsx代码 import { Component } from "react"; import '../component/list.less' import { searchCar } from '../api/index' import love from '../assets/爱心.gif'; import load from '../assets/images(1)/loading.gif' import arrow from '../assets/search/左箭头.gif' import

weixin_58495461的博客 1733
上一篇: reactjs-swiper 在react项目中实现轮播
下一篇: 从浏览器多进程到JS单线程,JS运行机制最全面的一次梳理
twinkle_J
博客等级 码龄8年 43粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值