-
Notifications
You must be signed in to change notification settings - Fork 7
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
[SP1] 활동후기 페이지 요청 많이 가는 에러 수정 #191
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import { useCallback, useEffect } from 'react'; | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import useBooleanState from '@src/hooks/useBooleanState'; | ||
import useStackedFetchBase from '@src/hooks/useStackedFetchBase'; | ||
import { api } from '@src/lib/api'; | ||
import { ExtraPart } from '@src/lib/types/universal'; | ||
import useInfiniteScroll from './useInfiniteScroll'; | ||
|
||
const useFetch = (selected: ExtraPart) => { | ||
const { ref, count, setCount } = useInfiniteScroll(); | ||
const jobRecordRef = useRef<Record<number, 'QUEUED' | 'DONE'>>({}); | ||
const isIncrementable = () => | ||
Object.values(jobRecordRef.current).every((value) => value === 'DONE'); | ||
|
||
const { ref, count, setCount } = useInfiniteScroll(isIncrementable); | ||
const [canGetMoreReviews, setCanGetMoreReviews, setCanNotGetMoreReviews] = useBooleanState(true); | ||
|
||
useEffect(() => { | ||
jobRecordRef.current[count] = 'QUEUED'; | ||
}, [count]); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이걸 willFetch 함수 안에서 하려 그랬는데, |
||
useEffect(() => { | ||
function initializeStates() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setCanGetMoreReviews, setCount 가 처음 컴포넌트 읽는 순간부터 unmount 시점까지 안변하나요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setCanGetMoreReviews가 잘못 이해한거라면 알려주세요...!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오오 생각 정리 좋아요! 이게 이전에는 set~ 함수 또한 변수고, 렌더링 때와 한번 마운트 되고의 값이 다르니 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 그렇게 setState 변수로 보아서 넣는 경우도 있군요! |
||
setCount(1); | ||
setCanGetMoreReviews(); | ||
for (const key in jobRecordRef.current) { | ||
if (Object.hasOwnProperty.call(jobRecordRef.current, key)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 페이지가 바뀌면 (예시: 전체 선택했다가 기획 선택하면) jobRecordRef.current 를 초기화해줍니다 ( |
||
delete jobRecordRef.current[key]; | ||
} | ||
} | ||
} | ||
Comment on lines
+24
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반복 돌지 않고 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 근간은 아니지만,, delete 키워드 찾다가 생각하게 된 것입니다😇 |
||
return () => { | ||
initializeStates(); | ||
|
@@ -23,6 +36,7 @@ const useFetch = (selected: ExtraPart) => { | |
setCanNotGetMoreReviews(); | ||
const response = await api.reviewAPI.getReviews(selected, count); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question; willFetch가 실행될때 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @f0rever0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useBooleanState 에서 canGetMoreReviews을 false로 만들어 주는 함수인데, const willFetch = useCallback(async () => {
setCanNotGetMoreReviews();
const response = await api.reviewAPI.getReviews(selected, count);
response.hasNextPage ? setCanGetMoreReviews() : setCanNotGetMoreReviews();
jobRecordRef.current[count] = 'DONE';
return response.reviews;
}, [selected, count, setCanGetMoreReviews, setCanNotGetMoreReviews]); |
||
response.hasNextPage ? setCanGetMoreReviews() : setCanNotGetMoreReviews(); | ||
jobRecordRef.current[count] = 'DONE'; | ||
return response.reviews; | ||
}, [selected, count, setCanGetMoreReviews, setCanNotGetMoreReviews]); | ||
const state = useStackedFetchBase(willFetch, count === 1); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { useState } from 'react'; | ||
import useIntersectionObserver from '@src/hooks/useIntersectionObserver'; | ||
|
||
export default function useInfiniteScroll() { | ||
export default function useInfiniteScroll(isIncrementable: () => boolean) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SeojinSeojin 그리고 () => boolean 타입인데 is~ 의 이름을 가지는 이유가 무엇일까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. observer box 를 없애는 방식에 썼던 변수가 제대로 위의 문제로 제대로 업데이트되지 않는 상황이었습니다 ..!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
맞아요 ....... [스크롤 이벤트]랑 [서버 요청]이랑 알잘딱 독립적으로 진행되도록 수정하는 것이 베스트일 것 같습니다 ..!!! 그러나 요청이 다 제대로 왔는지에 대한 상태를 확인하는 변수로부터 현재 스크롤 가능한지 확인하는 방법도 괜찮을 것 같다고 생각했습니다! 그럼 isIncrementable 을 훅에서 리턴해서, 컴포넌트에서 쓰도록 하는것이 좋을까요 ..? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
거의 변수처럼 쓰이는 함수이기 때문이었습니당 헤헤... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joohaem 좋은 인터페이스인가를 염려한 이유가 스크롤 이벤트에서 서버에서의 요청 상태를 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @f0rever0 C는 무엇이냐 하면, 훅의 인자나 interface나 마찬가지로 "개발"을 위해 뚫는 경우가 많아요. 이건 그 훅이나 객체가 가지는 정체성을 흐리는 행위고, 이렇게 뚫는 인터페이스가 많아질 수록 의존성이 엉키게 되면서 스파게티 코드가 될 가능성이 높아진다고 생각해요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joohaem 음 그렇군요!! 더 나은 해법이 있는지 고민해봐야겠네요. 설명 감사합니다 !!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
얕게나마 생각해보았지만 역시나만시나 서진이가 짠 방향보다 나은 것은 생각나지 않습니다... |
||
const [count, setCount] = useState(1); | ||
|
||
const ref = useIntersectionObserver( | ||
async (entry, observer) => { | ||
setCount((prevCount) => prevCount + 1); | ||
if (isIncrementable()) { | ||
setCount((prevCount) => prevCount + 1); | ||
} | ||
observer.unobserve(entry.target); | ||
}, | ||
{ rootMargin: '80px' }, | ||
|
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.
debounce 는 사용하지 않아서 지웟습니다 ..!!