-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: infinite scroll just working on frontend bottomsheet (#136)
* feat: infinite scroll just working on frontend in bottomsheet * chore: delete log
- Loading branch information
Showing
3 changed files
with
86 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import debounce from 'lodash.debounce' | ||
import { useState, useEffect, useCallback, useRef } from 'react' | ||
|
||
interface UseInfiniteScrollProps<T> { | ||
totalData: T[] | ||
itemsPerPage: number | ||
threshold?: number | ||
} | ||
|
||
const INITIAL_PAGE = 1 | ||
|
||
export const useInfiniteScroll = <T extends any>({ | ||
totalData, | ||
itemsPerPage, | ||
threshold = 100, | ||
}: UseInfiniteScrollProps<T>) => { | ||
const [data, setData] = useState<T[]>(totalData.slice(0, itemsPerPage)) | ||
const [page, setPage] = useState(INITIAL_PAGE) | ||
const [isLoading, setIsLoading] = useState(false) | ||
const [hasMore, setHasMore] = useState(totalData.length > itemsPerPage) | ||
const listRef = useRef<HTMLDivElement | null>(null) | ||
|
||
const loadMoreData = useCallback(() => { | ||
if (isLoading || !hasMore) return | ||
|
||
setIsLoading(true) | ||
setTimeout(() => { | ||
const startIndex = page * itemsPerPage | ||
const endIndex = startIndex + itemsPerPage | ||
const newData = totalData.slice(startIndex, endIndex) | ||
|
||
if (newData.length === 0) { | ||
setHasMore(false) | ||
} else { | ||
setData((prevData) => [...prevData, ...newData]) | ||
setPage((prevPage) => prevPage + 1) | ||
} | ||
|
||
setIsLoading(false) | ||
}, 100) | ||
}, [page, itemsPerPage, totalData, isLoading, hasMore]) | ||
|
||
const handleScroll = useCallback(() => { | ||
if (!listRef.current) return | ||
|
||
const { scrollTop, scrollHeight, clientHeight } = listRef.current | ||
if ( | ||
scrollHeight - scrollTop <= clientHeight + threshold && | ||
!isLoading && | ||
hasMore | ||
) { | ||
loadMoreData() | ||
} | ||
}, [isLoading, hasMore, loadMoreData, threshold]) | ||
|
||
useEffect(() => { | ||
const ref = listRef.current | ||
if (!ref) return | ||
|
||
const debouncedHandleScroll = debounce(handleScroll, 300) | ||
|
||
ref.addEventListener('scroll', debouncedHandleScroll) | ||
|
||
return () => { | ||
ref.removeEventListener('scroll', debouncedHandleScroll) | ||
} | ||
}, [handleScroll]) | ||
|
||
return { data, isLoading, listRef } | ||
} |