目录
简介
本文基于react实现下拉刷新效果,在下拉的时候会进入loading状态。
实现效果
效果如上图所示,在下拉到底部时候,会出现loading条,在处理完成后loading条消失。
具体代码
布局 & 逻辑
- import {useRef, useState} from "react";
-
- export const ScrollView = ({loadingComponent, contentComponent}) => {
- const LoadingComponent = loadingComponent;
- const ContentComponent = contentComponent;
-
- /**
- * 加载状态
- */
- const [loading, setLoading] = useState(false);
-
- /**
- * 滚动容器引用
- */
- const scrollRef = useRef(null);
-
- let contentStyle = {height: '30px', width:'100%', textAlign:'center', display:'none'};
-
- if (loading){ // 加载中显示
- contentStyle = {height: '30px', width:'100%', textAlign:'center'};
- scrollRef.current.scrollTop = 0; // 滚到头部
- }
-
- const handleScroll = ()=>{
- const {scrollTop} = scrollRef.current;
-
- if (scrollTop > 50 && !loading){
- setLoading(true); // 设置为加载中状态
-
- // 模拟数据加载
- setTimeout(()=>{
- setLoading(false); // 加载完成
- }, 3000)
- }
-
- }
- return (
- <div style={{height: '200px', overflow:'auto', width:'40%'}} ref={scrollRef} onScroll={handleScroll}>
- <div style={contentStyle}>
- <LoadingComponent/>
- </div>
- <div style={{height:'300px', width:'100%'}}>
- <ContentComponent/>
- </div>
- </div>
- )
- }
复制代码 使用demo
-
- import {ScrollView} from "./component/scroll-view/ScrollView";
-
- const App = ()=> {
-
- return (
- <ScrollView loadingComponent={Loading} contentComponent={Content}>
- </ScrollView>
- )
- }
- const Loading = ()=>{
- return <div>loading</div>
- }
- const Content = ()=>{
- return <div> hello, world</div>
- }
-
-
-
- export default App;
复制代码 番外:上拉加载实现
1、下载react-pullload 2、在组件中去引用 - import ReactPullLoad,{ STATS } from "react-pullload";
复制代码3、css样式
①引用插件内的样式 - import "node_modules/react-pullload/dist/ReactPullLoad.css";
复制代码②或者直接引入使用下列代码: 4、pullLoad标签包裹 - <ReactPullLoad
-
- downEnough={150}
-
- ref="reactpullload"
-
- className="block"
-
- *加上下面注释的属性会出问题
-
- // isBlockContainer={true}
-
- action={this.state.action}
-
- handleAction={this.handleAction}
-
- hasMore={this.state.hasMore}
-
- style={{paddingTop: 132}}
-
- distanceBottom={1000}>
-
- ..... this is list
-
- </ReactPullLoad>
复制代码5、scroll函数 - constructor(props) {
-
- super(props);
-
- this.state = {
-
- // scroll 相关
-
- hasMore: true,
-
- action: STATS.init,
-
- }
-
- }
-
- // 滚动条
-
- handleAction = (action) => {
-
- if(action === this.state.action ||
-
- action === STATS.refreshing && this.state.action === STATS.loading ||
-
- action === STATS.loading && this.state.action === STATS.refreshing){
-
- return false
-
- }
-
- if(action === STATS.refreshing){//刷新
-
- setTimeout(()=>{
-
- //refreshing complete
-
- 下拉刷新
-
- }, 1000)
-
- } else if(action === STATS.loading){//加载更多
-
- this.setState({
-
- hasMore: true
-
- });
-
- setTimeout(()=>{
-
- if(this.state.index === this.state.curPage){
-
- this.setState({
-
- action: STATS.reset,
-
- hasMore: false
-
- });
-
- } else{
-
- 上拉加载....
-
- }
-
- }, 1000)
-
- }
-
- this.setState({
-
- action: action
-
- })
-
- }
复制代码到此这篇关于基于React实现下拉刷新效果的文章就介绍到这了,更多相关React下拉刷新内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |