相信有很多小伙伴发现antd-mobile中的下拉刷新组件,也发现例子挺难的,其实这个组件并没有那么复杂,只是demo例子不好理解,给大家提供一个简单的demo,或许可以帮到你

上拉刷新下拉加载

- 因为它用react-dom做操作了,所以把react-dom也导进来
- rowID是每次的ID,rowData是每次的数据
- renderSseprator就是个分隔符
- this.state.dataSource就是数据源
- 外围的那个const的dataSource是一种数据结构,它有一个方法叫cloneWithRows
- Button没用,删就完了
- renderFooter是为了做上拉刷新时的Loading效果
- 第一步是通过dataSource去拿数据,第二步是通过render(row)去渲染那个模板
- rowData是每一页的数据,就是每次装载进来的数据,rowID是它帮你封装好的,直接在key={rowID}用就行,
在DidMount整完数据以后在这边的rouData就是你的state.dataSource里面的数据(第一页)
- renderSeparator 就是刚开始他们行和行之间的分隔符
- pageSize是刷新的时候一次显示几条数据

附上代码,可直接复制过去,根据你的需求更改

import React,{ Component } from 'react'
import ReactDOM from 'react-dom' //下拉刷新组件依赖react-dom,所以需要将其引进来 import { PullToRefresh, ListView } from 'antd-mobile'; class ListContainer extends Component {
constructor(props) {
super(props);
const dataSource = new ListView.DataSource({ //这个dataSource有cloneWithRows方法
rowHasChanged: (row1, row2) => row1 !== row2,
}); this.pageNo = 0 //定义分页信息
this.state = {
dataSource,
refreshing: true,
isLoading: true,
height: document.documentElement.clientHeight,
useBodyScroll: false,
hasMore: true
};
} componentDidUpdate() {
if (this.state.useBodyScroll) {
document.body.style.overflow = 'auto';
} else {
document.body.style.overflow = 'hidden';
}
} async componentDidMount() {
const hei = this.state.height - ReactDOM.findDOMNode(this.lv).offsetTop; this.rData = (await this.genData()).sceneryinfo;
console.log(this.rData)
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.rData),
height: hei,
refreshing: false,
isLoading: false,
});
}
genData(){ //请求数据的方法
this.pageNo++ //每次下拉的时候pageNo++
return fetch('/scenery/json/scenerynearycitylist.html?CountryId=0&ProvinceId=3&CityId=53&LbTypes=&sorttype=0&page='+this.pageNo+'&gradeid=0&themeid=0&pricerange=0&issearchbytimenow=0&paytype=0&range=0&keyword=0&IsGlobal=0&IsYiYuan=0&cityArea=0&lat=0&lon=0',
{
method: 'GET',
headers: {
'content-type': 'application/json'
},
})
.then(response => response.json())
.then(function(result) {
if(result){
return result
}else{
this.setState({
hasMore: false
})
}
})
} onRefresh = () => {
// this.setState({ refreshing: true, isLoading: true });
// // simulate initial Ajax
// setTimeout(() => {
// this.rData = genData();
// this.setState({
// dataSource: this.state.dataSource.cloneWithRows(this.rData),
// refreshing: false,
// isLoading: false,
// });
// }, 600);
}; onEndReached = async (event) => {
// load new data
// hasMore: from backend data, indicates whether it is the last page, here is false
if (this.state.isLoading && !this.state.hasMore) {
return;
} //如果this.state.hasMore为false,说明没数据了,直接返回
console.log('reach end', event);
this.setState({ isLoading: true });
this.rData = [...this.rData, ...((await this.genData()).sceneryinfo)]; //每次下拉之后将新数据装填过来
this.setState({
dataSource: this.state.dataSource.cloneWithRows(this.rData),
isLoading: false,
});
}; render() {
//这里就是个渲染数据,rowData就是每次过来的那一批数据,已经自动给你遍历好了,rouID可以作为key值使用,直接渲染数据即可
const row = (rowData, sectionID, rowID) => {
return (
<div key={rowID} style={{"height":"100px"}}>{rowData.name}</div>
);
};
return (
<div>
<ListView
key={this.state.useBodyScroll ? '0' : '1'}
ref={el => this.lv = el}
dataSource={this.state.dataSource}
renderFooter={ //renderFooter就是下拉时候的loading效果,这里的内容可以自己随需求更改
() => (
<div style={{ padding: 30, textAlign: 'center' }}>
{this.state.isLoading ? 'Loading...' : 'Loaded'}
</div>
)
}
renderRow={row} //渲染你上边写好的那个row
useBodyScroll={this.state.useBodyScroll}
style={this.state.useBodyScroll ? {} : {
height: this.state.height,
border: '1px solid #ddd',
margin: '5px 0',
}}
pullToRefresh={<PullToRefresh
refreshing={this.state.refreshing}
onRefresh={this.onRefresh}
/>}
onEndReached={this.onEndReached}
pageSize={8} //每次下拉之后显示的数据条数
/>
</div>
);
}
} export default ListContainer

如果说你看到了这里,是不是觉得还是有点东西的,如果你觉得帮到你了,给个评论鼓励鼓励孩子吧,发了这么多到现在就一条评论,挺可怜的。后续给大家发一个在下拉刷新组件中如何使用redux,感谢查阅.

关于antd-mobile中列表上拉加载PullToRefresh的使用的更多相关文章

  1. reactnative中FlatList上拉加载更多的解决办法

    项目app中用到了list滚动加载,把List做了下对比发现FlatList比较适合自己的项目,但是在实际运用中 onEndReached方法需要给定 onEndReachedThreshold的高度 ...

  2. iOS MJRefresh的使用 (列表上拉加载更多)

    pod 'MJRefresh' import MJRefresh 加载更多 let footView = MJRefreshAutoNormalFooter(refreshingBlock:{ //去 ...

  3. Android 下拉刷新上拉加载PullToRefresh

    https://github.com/823546371/PullToRefresh http://www.jianshu.com/p/0f5d0991efdc

  4. Flutter实战视频-移动电商-35.列表页_上拉加载更多制作

    35.列表页_上拉加载更多制作 右侧列表上拉加载配合类别的切换 上拉加载需要一个page参数,当点击大类或者小类的时候,这个page就要变成1 provide内定义参数 首先我们需要定义一个page的 ...

  5. wepy小程序实现列表分页上拉加载(2)

    第一篇:wepy小程序实现列表分页上拉加载(1) 本文接着上一篇内容: 4.优化-添加加载动画 (1)首先写加载动画的结构和样式 打开list.wpy文件 template结构代码: <temp ...

  6. Flutter移动电商实战 --(35)列表页_上拉加载更多制作

    右侧列表上拉加载配合类别的切换 上拉加载需要一个page参数,当点击大类或者小类的时候,这个page就要变成1 provide内定义参数 首先我们需要定义一个page的变量 下图是我们之前在首页的时候 ...

  7. 移动端上拉加载下拉刷新插件-mescroll.js插件

    官网地址是:http://www.mescroll.com // 初始化mescroll function initMeScroll() { //创建MeScroll对象,内部已默认开启下拉刷新,自动 ...

  8. Flutter实战视频-移动电商-20.首页_火爆专区上拉加载效果

    20.首页_火爆专区上拉加载效果 上拉加载的插件比较都.没有一个一枝独秀的 可以自定义酷炫的header和footer 一直在更新 推荐使用这个插件: https://github.com/xuelo ...

  9. [RN] React Native 实现 FlatList上拉加载

     FlatList可以利用官方组件 RefreshControl实现下拉刷新功能,但官方没有提供相应的上拉加载的组件,因此在RN中实现上拉加载比下拉刷新要复杂一点. 不过我们仍可以通过FlatList ...

随机推荐

  1. C++基础--结构体声名

    struct是一种数据结构,当需要存储的相关数据为一个集合时,struct是很好的选择;例如,当存储student,学生的学号, 名字,年龄,身高,就构成了一个集合,用stuct声名为: typede ...

  2. Linux在Tomcat下部署JavaWeb项目

    一.Linux快速部署War包操作 1.先关闭Tomcat /home/java/tomcat8/bin/shutdown.sh 注意:进入tomcat bin目录下操作 2.进入War包存放目录(可 ...

  3. 解决C#调试ArcMap断点不能停的问题

    问题出在ArcMap bin\ArcMap.exe.config 默认是不支持NET4.0 <startup> <!--<supportedRuntime version=&q ...

  4. ASP.NET中引用dll“找不到指定模块"的完美解决办法 z

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息.DllImport属性应用于方法,要求最少要 ...

  5. WAKE-LINUX-SOFT-linux安装,配置,基础

    1,ubuntu 1,1下载,安装 中文ubuntu站,http://cn.ubuntu.com/ 下载地址:https://www.ubuntu.com/download 安装手册:https:// ...

  6. 源码安装mysql5.6.37

    MYSQL 源码安装: 修改参数文件:vi /etc/security/limits.confmysql soft nproc 2047mysql hard nproc 16384mysql soft ...

  7. March 2 2017 Week 9 Thursday

    The first duty of love is to listen. 爱的首要责任是倾听. Yesterday, I read an article that says a successful ...

  8. 在linux代码中打印函数调用的堆栈的方法

    之前一直有这样的需求,当时问到,也没搜到方法,现在竟然既问到了,也搜到了,哎,世事真是不能强求啊! 在Linux内核调试中,经常用到的打印函数调用堆栈的方法非常简单,只需在需要查看堆栈的函数中加入: ...

  9. Kubernetes里的secret最基本的用法

    Secret解决了密码.token.密钥等敏感数据的配置问题,使用Secret可以避免把这些敏感数据以明文的形式暴露到镜像或者Pod Spec中. Secret可以以Volume或者环境变量的方式使用 ...

  10. Jmeter入门2 http请求—简单的get请求

    发送一个简单的get http请求 1 启动Jmeter,在测试计划上点击鼠标右键>添加>Threads(Users)>线程组 2 线程组界面.可设置线程数,几秒启动所有线程,循环次 ...