-
Notifications
You must be signed in to change notification settings - Fork 0
[week8/mission]Debouncing & Throttling #40
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
Conversation
LP/src/pages/SearchResult.tsx
Outdated
| 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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금은 리렌더링 시 throttledFetchNext가 매번 새 함수를 리턴해, useEffect의 의존성 배열에서 throttledFetchNext가 계속 바뀌게 돼서, 리렌더링마다 handleScroll 이벤트가 제거되고 새로 등록돼요.
이뿐만 아니라, useThrottle 훅 내에서 lastCall.current도 매번 새로 초기화되기 때문에,
쓰로틀링 효과가 나타나지 않아요!
두가지 방법 중 하나 사용하는 걸 추천드립니당
- useThrottle로 만든 함수를 useRef(...).current에 저장해서 리렌더링돼도 같은 함수 참조를 유지하기
- useThrottle 훅 내에서 useCallback({리턴 함수}, [delay]) as T 로 리턴해 리렌더링 시에도 함수값 유지되게 하기
namgungseok12
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good!
qowldud
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수고하셨씁니당
|
|
||
| const [inputValue, setInputValue] = useState(search); | ||
| const [searchType, setSearchType] = useState<"title" | "tag">( | ||
| searchParams.get("type") === "tag" ? "tag" : "title" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 태그까지는 생각안했어서 이런방식으로 하는거 좋은것같아요!
| queryKey: ["search", debouncedSearch, sortOrder, searchType], | ||
| queryFn: async ({ pageParam = 0 }) => { | ||
| console.log("fetching LPs..."); | ||
| const res = await api.get("/lps", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
태그 관련은 api가 /lps/tag여서 안불러와지던 것일까요..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오옹 그런가바여 참고해서 고쳐볼게요!!
✏️ 작업 내용
#️⃣ 39
#39
📷 작업 결과
💡 함께 공유하고 싶은 부분
🤔 질문
✅ 워크북 체크리스트
✅ 컨벤션 체크리스트