官網(wǎng)地址:https://github.com/ankeetmaini/react-infinite-scroll-component
記得自己曾經(jīng)弱弱的發(fā)問,為什么上拉加載更多會觸發(fā)多次。
1 上拉加載更多
默認以body/window為容器
import React, { useState, useEffect } from 'react';
import request from '@/utils/request';
import InfiniteScroll from 'react-infinite-scroll-component';
export default () => {
const [list, setList] = useState([]);
const requestList = () => {
setTimeout(async () => {
const response = await request.get('/api/notes/list', {
params: {
name: 12,
},
});
setList(list.concat(response.list));
}, 1000);
};
useEffect(() => {
requestList();
}, []);
return (
<div>
<InfiniteScroll
dataLength={list.length}
next={requestList}
hasMore={false}
endMessage={
<p style={{ textAlign: 'center' }}>
<b>Yay! You have seen it all</b>
</p>
}
loader={<h4>Loading...</h4>}
>
{list.map((item, index) => (
<div style={{ height: 100 }} key={index}>
{item.id}
</div>
))}
</InfiniteScroll>
</div>
);
};
效果如圖
屬性 | 作用 | 注意事項 |
---|---|---|
dataLength | 當您上拉時紧索,程序會根據(jù)這個值有沒有發(fā)生變化決定是否觸發(fā)next
|
當數(shù)據(jù)發(fā)生變化的間隔非常小時灯蝴,是會觸發(fā)多次的虑啤,比如快速的把滾動條拖到底 |
next | 用戶上拉達到閾值時用于加載更多數(shù)據(jù) | |
hasMore | 是否還有更多的數(shù)據(jù) | 為false 時next 不會觸發(fā) |
endMessage | 沒有更多數(shù)據(jù)時上拉到底部會顯示 | 需要hasMore 為false
|
scrollThreshold | 上拉時觸發(fā)next ,相當于底部的距離 |
默認是0.8咏瑟,您可以設(shè)置自己的值尚卫,比如200px 酪捡,則會按照 100%-200px 叁征,顯然,隨著滾動區(qū)域越來越高逛薇,100%越來越大捺疼,200固定不變,意味著越往后您越要上拉的更接近底部才會觸發(fā)next
|
在指定區(qū)域內(nèi)觸發(fā)
只需要使用height
...
<InfiniteScroll
...
height={400}
...
>
...
效果
指定滾動的父容器
需要用到scrollableTarget
永罚,這時候InfiniteScroll
就沒必要指定height
<div
id="scrollableDiv"
style={{ height: 300, overflow: 'auto' }}
>
<InfiniteScroll
...
scrollableTarget="scrollableDiv"
...
>
...
</div>
下拉刷新
import React, { useState, useEffect } from 'react';
import request from '@/utils/request';
import InfiniteScroll from 'react-infinite-scroll-component';
export default () => {
const [list, setList] = useState([]);
const requestList = () => {
setTimeout(async () => {
const response = await request.get('/api/notes/list', {
params: {
name: 12,
},
});
setList(list.concat(response.list));
}, 1000);
};
useEffect(() => {
requestList();
}, []);
return (
<InfiniteScroll
dataLength={list.length} //This is important field to render the next data
next={requestList}
hasMore={true}
loader={<h4>Loading...</h4>}
refreshFunction={requestList}
pullDownToRefresh
pullDownToRefreshThreshold={10}
pullDownToRefreshContent={
<h3 style={{ textAlign: 'center' }}>
↓ Pull down to refresh
</h3>
}
releaseToRefreshContent={
<h3 style={{ textAlign: 'center' }}>
↑ Release to refresh
</h3>
}
>
{list.map((item, index) => (
<div style={{ height: 100 }} key={index}>
{item.id}
</div>
))}
</InfiniteScroll>
);
};
這個要在移動端看啤呼,PC端我在mac筆記本上用 三指下拉也看到過
import React, { useState, useEffect } from 'react';
import request from '@/utils/request';
import InfiniteScroll from 'react-infinite-scroll-component';
export default () => {
const [list, setList] = useState([]);
const requestList = () => {
setTimeout(async () => {
const response = await request.get('/api/notes/list', {
params: {
name: 12,
},
});
setList(list.concat(response.list));
}, 1000);
};
useEffect(() => {
requestList();
}, []);
return (
<InfiniteScroll
style={{ marginTop: 120 }}
height={400}
dataLength={list.length} //This is important field to render the next data
next={requestList}
hasMore={true}
loader={<h4>Loading...</h4>}
refreshFunction={requestList}
pullDownToRefresh
pullDownToRefreshThreshold={30}
pullDownToRefreshContent={
<h3 style={{ textAlign: 'center' }}>
↓ Pull down to refresh
</h3>
}
releaseToRefreshContent={
<h3 style={{ textAlign: 'center' }}>
↑ Release to refresh
</h3>
}
>
{list.map((item, index) => (
<div style={{ height: 100 }} key={index}>
{item.id}
</div>
))}
</InfiniteScroll>
);
};
屬性 | 作用 | 注意事項 |
---|---|---|
pullDownToRefresh | 是否開啟下拉刷新,默認為false
|
|
refreshFunction | 觸發(fā)下拉刷新時調(diào)用的函數(shù) | |
pullDownToRefreshContent | 未達到下拉閾值顯示的內(nèi)容 | |
releaseToRefreshContent | 達到下拉閾值顯示的內(nèi)容 |
效果如下
其余的一些屬性
name | type | description |
---|---|---|
children | node (list) | the data items which you need to scroll. |
onScroll | function | a function that will listen to the scroll event on the scrolling container. Note that the scroll event is throttled, so you may not receive as many events as you would expect. |
className | string | add any custom class you want |
style | object | any style which you want to override |
hasChildren | bool | children is by default assumed to be of type array and it's length is used to determine if loader needs to be shown or not, if your children is not an array, specify this prop to tell if your items are 0 or more. |
initialScrollY | number | set a scroll y position for the component to render with. |
key | string | the key for the current data set being shown, used when the same component can show different data sets at different times, default=undefined |