-
Notifications
You must be signed in to change notification settings - Fork 1
Fix(client): 직무 아티클 무한 스크롤 중복 호출 버그 해결 #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b142066
setting: add react-intersection-observer
constantly-dev 1974109
refactor: useInfiniteScroll 내부 useInView 사용하도록 변경
constantly-dev 17cf360
feat: 바뀐 useInfiniteScroll interface 적용
constantly-dev c7b06af
fix: getNextPageParam 실행 조건 수정
constantly-dev 78d06ea
chore: 불필요 코드 제거
constantly-dev ac30c77
fix(client): pass scroll root directly to useInView in useInfiniteScroll
constantly-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,51 +1,52 @@ | ||
| import { useEffect, useRef } from 'react'; | ||
| import { useInView } from 'react-intersection-observer'; | ||
|
|
||
| interface UseInfiniteScrollProps { | ||
| fetchNextPage: () => void; | ||
| hasNextPage?: boolean; | ||
| root?: React.RefObject<HTMLElement | null>; | ||
| threshold?: number; | ||
| } | ||
| type UseInfiniteScrollProps = { | ||
| /** 더 불러올 데이터가 있는지 */ | ||
| hasMore?: boolean; | ||
| /** 추가 로딩 중인지 (중복 호출 방지용) */ | ||
| isLoadingMore?: boolean; | ||
| /** 다음 데이터를 불러오는 함수 */ | ||
| loadMore: () => void | Promise<unknown>; | ||
|
|
||
| /** | ||
| * Intersection Observer를 사용하여 무한 스크롤을 구현하는 커스텀 훅 | ||
| * @returns observer가 감지할 엘리먼트에 연결할 ref 객체 | ||
| */ | ||
| /** 내부 스크롤 컨테이너를 root로 쓰고 싶을 때 */ | ||
| rootRef?: React.RefObject<HTMLElement | null>; | ||
|
|
||
| /** 바닥에 닿기 전에 미리 불러오기 (ex: '200px 0px') */ | ||
| rootMargin?: string; | ||
|
|
||
| /** 무한 스크롤을 일시적으로 끄기 */ | ||
| enabled?: boolean; | ||
| }; | ||
|
|
||
| export const useInfiniteScroll = ({ | ||
| fetchNextPage, | ||
| hasNextPage, | ||
| root, | ||
| threshold = 0.5, | ||
| hasMore, | ||
| isLoadingMore = false, | ||
| loadMore, | ||
| rootRef, | ||
| rootMargin = '200px 0px', | ||
| enabled = true, | ||
| }: UseInfiniteScrollProps) => { | ||
| const targetRef = useRef<HTMLDivElement>(null); | ||
| const lockRef = useRef(false); | ||
|
|
||
| const { ref, inView } = useInView({ | ||
| root: rootRef?.current ?? null, | ||
| threshold: 0, | ||
| rootMargin, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (!hasNextPage) return; | ||
|
|
||
| const observer = new IntersectionObserver( | ||
| ([entry]) => { | ||
| if (entry.isIntersecting) { | ||
| fetchNextPage(); | ||
| } | ||
| }, | ||
| { | ||
| root: root?.current, | ||
| threshold, | ||
| } | ||
| ); | ||
|
|
||
| const currentTarget = targetRef.current; | ||
| if (currentTarget) { | ||
| observer.observe(currentTarget); | ||
| } | ||
|
|
||
| return () => { | ||
| if (currentTarget) { | ||
| observer.unobserve(currentTarget); | ||
| } | ||
| }; | ||
| }, [fetchNextPage, hasNextPage, root, threshold]); | ||
|
|
||
| return targetRef; | ||
| if (!enabled) return; | ||
| if (!inView) return; | ||
| if (!hasMore) return; | ||
| if (isLoadingMore) return; | ||
| if (lockRef.current) return; | ||
|
|
||
| lockRef.current = true; | ||
| Promise.resolve(loadMore()).finally(() => { | ||
| lockRef.current = false; | ||
| }); | ||
| }, [enabled, inView, hasMore, isLoadingMore, loadMore]); | ||
|
|
||
| return ref; | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.