npm install --save react-infinite-scroll-component
or
yarn add react-infinite-scroll-component
// in code ES6
import InfiniteScroll from 'react-infinite-scroll-component';
// or commonjs
var InfiniteScroll = require('react-infinite-scroll-component');
사용하는 패키지 매니저에서 react-infinite-scroll-component를 설치 후 import 한다.
<InfiniteScroll
dataLength={items.length} //This is important field to render the next data
next={fetchData}
hasMore={true}
loader={<h4>Loading...</h4>}
endMessage={
<p style={{ textAlign: 'center' }}>
<b>Yay! You have seen it all</b>
</p>
}
// below props only if you need pull down functionality
refreshFunction={this.refresh}
pullDownToRefresh
pullDownToRefreshThreshold={50}
pullDownToRefreshContent={
<h3 style={{ textAlign: 'center' }}>↓ Pull down to refresh</h3>
}
releaseToRefreshContent={
<h3 style={{ textAlign: 'center' }}>↑ Release to refresh</h3>
}
>
{items}
</InfiniteScroll>
dataLength에서 로드 할 데이터의 길이를 정해주고, next에서 추가 데이터를 불러오는 로직을 구현해주면 된다.
ref : https://www.npmjs.com/package/react-infinite-scroll-component
'React' 카테고리의 다른 글
[React] opacity 값이 빌드 후 1%로 변경되는 현상 해결방법 (0) | 2023.07.21 |
---|---|
[React] React Hook useEffect has a missing dependency (0) | 2021.07.20 |