-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: load transaction list in batches to enable infinite scroll
- Loading branch information
1 parent
b741925
commit cc08fb6
Showing
3 changed files
with
102 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
type Props = { | ||
children: React.ReactNode | ||
batchSize: number | ||
onLoadMore: (offset: number) => Promise<boolean> | ||
} | ||
|
||
const InfiniteScroll = ({ children, batchSize, onLoadMore }: Props) => { | ||
const [batchOffset, setBatchOffset] = useState(0) | ||
const [hasMore, setHasMore] = useState(true) | ||
|
||
// load more items whenever batchOffset is changed | ||
useEffect(() => { | ||
// don't run on initial render or when there are no more items to load | ||
if (batchOffset !== 0 && hasMore) { | ||
onLoadMore(batchOffset).then(setHasMore) | ||
} | ||
}, [batchOffset]) // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
const onScroll = () => { | ||
const scrollTop = document.documentElement.scrollTop | ||
const scrollHeight = document.documentElement.scrollHeight | ||
const clientHeight = document.documentElement.clientHeight | ||
|
||
if (scrollTop + clientHeight >= scrollHeight) { | ||
setBatchOffset(batchOffset + batchSize) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', onScroll) | ||
return () => window.removeEventListener('scroll', onScroll) | ||
}) | ||
|
||
return children | ||
} | ||
|
||
export default InfiniteScroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters