基于React實現(xiàn)下拉刷新效果
簡介
本文基于react實現(xiàn)下拉刷新效果,在下拉的時候會進(jìn)入loading狀態(tài)。
實現(xiàn)效果

效果如上圖所示,在下拉到底部時候,會出現(xiàn)loading條,在處理完成后loading條消失。
具體代碼
布局 & 邏輯
import {useRef, useState} from "react";
export const ScrollView = ({loadingComponent, contentComponent}) => {
const LoadingComponent = loadingComponent;
const ContentComponent = contentComponent;
/**
* 加載狀態(tài)
*/
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); // 設(shè)置為加載中狀態(tài)
// 模擬數(shù)據(jù)加載
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;番外:上拉加載實現(xiàn)
1、下載react-pullload
npm i react-pullload
2、在組件中去引用
import ReactPullLoad,{ STATS } from "react-pullload";3、css樣式
①引用插件內(nèi)的樣式
import "node_modules/react-pullload/dist/ReactPullLoad.css";
②或者直接引入使用下列代碼:
.pull-load {
position: relative;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}
.pull-load-head {
position: absolute;
transform: translate3d(0px, -100%, 0px);
width: 100%;
}
.state-refreshing .pull-load-head,
.state-refreshed .pull-load-head {
position: relative;
transform: none;
}
.pull-load-body {
position: relative;
}
.state-refreshing .pull-load-body {
transition: transform 0.2s;
}
.state-reset .pull-load-body {
transition: transform 0.2s;
}
.pull-load-head-default {
text-align: center;
font-size: 12px;
line-height: 0.8rem;
color: #7676a1;
}
.state-pulling .pull-load-head-default:after {
content: '下拉刷新';
}
.state-pulling.enough .pull-load-head-default:after {
content: '松開刷新';
}
.state-refreshing .pull-load-head-default:after {
content: '正在刷新...';
}
.state-refreshed .pull-load-head-default:after {
content: '刷新成功';
}
.state-pulling .pull-load-head-default {
opacity: 1;
}
.state-pulling .pull-load-head-default i {
display: inline-block;
font-size: 0.3rem;
margin-right: .6em;
margin-top: -3px;
vertical-align: middle;
height: 0.3rem;
border-left: 1px solid;
position: relative;
transition: transform .3s ease;
}
.state-pulling .pull-load-head-default i:before,
.state-pulling .pull-load-head-default i:after {
content: '';
position: absolute;
font-size: .5em;
width: 1em;
bottom: 0px;
border-top: 1px solid;
}
.state-pulling .pull-load-head-default i:before {
right: 1px;
transform: rotate(50deg);
transform-origin: right;
}
.state-pulling .pull-load-head-default i:after {
left: 0px;
transform: rotate(-50deg);
transform-origin: left;
}
.state-pulling.enough .pull-load-head-default i {
transform: rotate(180deg);
}
.state-refreshing .pull-load-head-default i {
margin-right: 10px;
margin-top: -3px;
display: inline-block;
vertical-align: middle;
font-size: 0.3rem;
width: 0.3rem;
height: 0.3rem;
border: 2px solid #9494b6;
border-top-color: #fff;
border-radius: 100%;
animation: circle .8s infinite linear;
}
.state-refreshed .pull-load-head-default {
opacity: 1;
transition: opacity 1s;
}
.state-refreshed .pull-load-head-default i {
display: inline-block;
box-sizing: content-box;
vertical-align: middle;
margin-right: 10px;
margin-top: -3px;
font-size: 14px;
height: 1em;
width: 1em;
border: 2px solid;
border-radius: 100%;
position: relative;
}
.state-refreshed .pull-load-head-default i:before {
content: '';
position: absolute;
top: -2px;
left: 4px;
height: 11px;
width: 5px;
border: solid;
border-width: 0 2px 2px 0;
transform: rotate(40deg);
}
.pull-load-footer-default {
text-align: center;
font-size: 12px;
line-height: 0.8rem;
color: #7676a1;
}
.state-loading .pull-load-footer-default:after {
content: '加載更多';
}
.pull-load-footer-default.nomore:after {
content: '沒有更多';
}
.state-loading .pull-load-footer-default i {
margin-right: 10px;
margin-top: -3px;
display: inline-block;
vertical-align: middle;
font-size: 0.3rem;
width: 0.3rem;
height: 0.3rem;
border: 2px solid #9494b6;
border-top-color: #fff;
border-radius: 100%;
animation: circle .8s infinite linear;
}
@keyframes circle {
100% {
transform: rotate(360deg);
}
}4、pullLoad標(biāo)簽包裹
<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函數(shù)
constructor(props) {
super(props);
this.state = {
// scroll 相關(guān)
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
})
}到此這篇關(guān)于基于React實現(xiàn)下拉刷新效果的文章就介紹到這了,更多相關(guān)React下拉刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React+TypeScript+webpack4多入口配置詳解
這篇文章主要介紹了React+TypeScript+webpack4多入口配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
React實現(xiàn)組件間通信的幾種方式小結(jié)
在React應(yīng)用中,組件間的通信是一個基礎(chǔ)而關(guān)鍵的概念,理解和掌握不同組件之間的通信方式,可以幫助我們構(gòu)建出更加模塊化、可維護(hù)和可擴(kuò)展的應(yīng)用程序,React提供了多種組件通信的方法,本文給大家詳細(xì)的介紹了這些方法,需要的朋友可以參考下2024-07-07
React Scheduler 最小堆實現(xiàn)小結(jié)
本文主要介紹了React Scheduler 最小堆實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2026-01-01
react-redux多個組件數(shù)據(jù)共享的方法
這篇文章主要介紹了react-redux多個組件數(shù)據(jù)共享的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
react實現(xiàn)導(dǎo)航欄二級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了react實現(xiàn)導(dǎo)航欄二級聯(lián)動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

