Conversation
duwlsssss
approved these changes
May 23, 2025
LP/src/pages/SearchResult.tsx
Outdated
Comment on lines
70
to
88
| const throttledFetchNext = useThrottle(() => { | ||
| if (hasNextPage && !isFetchingNextPage) { | ||
| fetchNextPage(); | ||
| } | ||
| }, 1000); | ||
|
|
||
| useEffect(() => { | ||
| const handleScroll = () => { | ||
| if ( | ||
| window.innerHeight + window.scrollY >= | ||
| document.body.offsetHeight - 300 | ||
| ) { | ||
| throttledFetchNext(); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener("scroll", handleScroll); | ||
| return () => window.removeEventListener("scroll", handleScroll); | ||
| }, [throttledFetchNext]); |
Contributor
There was a problem hiding this comment.
지금은 리렌더링 시 throttledFetchNext가 매번 새 함수를 리턴해, useEffect의 의존성 배열에서 throttledFetchNext가 계속 바뀌게 돼서, 리렌더링마다 handleScroll 이벤트가 제거되고 새로 등록돼요.
이뿐만 아니라, useThrottle 훅 내에서 lastCall.current도 매번 새로 초기화되기 때문에,
쓰로틀링 효과가 나타나지 않아요!
두가지 방법 중 하나 사용하는 걸 추천드립니당
- useThrottle로 만든 함수를 useRef(...).current에 저장해서 리렌더링돼도 같은 함수 참조를 유지하기
- useThrottle 훅 내에서 useCallback({리턴 함수}, [delay]) as T 로 리턴해 리렌더링 시에도 함수값 유지되게 하기
qowldud
approved these changes
May 23, 2025
|
|
||
| const [inputValue, setInputValue] = useState(search); | ||
| const [searchType, setSearchType] = useState<"title" | "tag">( | ||
| searchParams.get("type") === "tag" ? "tag" : "title" |
| queryKey: ["search", debouncedSearch, sortOrder, searchType], | ||
| queryFn: async ({ pageParam = 0 }) => { | ||
| console.log("fetching LPs..."); | ||
| const res = await api.get("/lps", { |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
✏️ 작업 내용
#️⃣ 39
#39
📷 작업 결과
💡 함께 공유하고 싶은 부분
🤔 질문
✅ 워크북 체크리스트
✅ 컨벤션 체크리스트