-
Notifications
You must be signed in to change notification settings - Fork 0
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
[FE] feat: 리뷰 좋아요 디바운스 적용 #377
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as useReviewFavoriteMutation } from './useReviewFavoriteMutation'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useMutation } from '@tanstack/react-query'; | ||
|
||
import { productApi } from '@/apis'; | ||
import type { ReviewFavoriteRequestBody } from '@/types/review'; | ||
|
||
const headers = { 'Content-Type': 'application/json' }; | ||
|
||
const patchReviewFavorite = (productId: number, reviewId: number, body: ReviewFavoriteRequestBody) => { | ||
return productApi.patch({ params: `/${productId}/reviews/${reviewId}`, credentials: true }, headers, body); | ||
}; | ||
|
||
const useReviewFavoriteMutation = (productId: number, reviewId: number) => { | ||
return useMutation({ | ||
mutationFn: (body: ReviewFavoriteRequestBody) => patchReviewFavorite(productId, reviewId, body), | ||
}); | ||
}; | ||
|
||
export default useReviewFavoriteMutation; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
const useDebounce = (fn: Function, ms = 0) => { | ||
const timeoutRef = useRef<NodeJS.Timeout | null>(null); | ||
const callbackRef = useRef(fn); | ||
|
||
const debounce = useCallback(() => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
|
||
timeoutRef.current = setTimeout(() => { | ||
callbackRef.current(); | ||
}, ms); | ||
}, [ms]); | ||
|
||
const clear = useCallback(() => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
callbackRef.current = fn; | ||
}, [fn]); | ||
|
||
useEffect(() => { | ||
return clear; | ||
}, []); | ||
|
||
return [debounce, clear]; | ||
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. 쓰는 곳에서 자유롭게 이름을 수정하기 위해 작성했습니다 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. ㅋㅋㅋㅋㅋ 알겠습니다 |
||
}; | ||
|
||
export default useDebounce; |
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.
clear만 따로 useEffect 선언해주는 이유는 뭔가요??
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.
언마운트 될 때 타임아웃 클리어 하기 위해 작성했읍니다!