Skip to content

Commit 97eeb0a

Browse files
committed
Refact: 디바운스 적용해 스크롤 이벤트 제어
1 parent ae9a18e commit 97eeb0a

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"dayjs": "^1.11.12",
1818
"firebase": "^10.13.0",
1919
"heic2any": "^0.0.4",
20+
"lodash": "^4.17.21",
2021
"react": "^18.3.1",
2122
"react-dom": "^18.3.1",
22-
"react-responsive": "^10.0.0",
23+
"react-responsive": "^10.0.0",
2324
"react-router-dom": "^6.24.1",
2425
"recoil": "^0.7.7",
2526
"recoil-persist": "^5.1.0",
@@ -29,6 +30,7 @@
2930
"swiper": "^11.1.8"
3031
},
3132
"devDependencies": {
33+
"@types/lodash": "^4.17.13",
3234
"@types/node": "^20.14.10",
3335
"@types/react": "^18.3.3",
3436
"@types/react-dom": "^18.3.0",

src/pages/Home/OOTD/index.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { PostSummary } from '@apis/post/dto';
66
import { handleError } from '@apis/util/handleError';
77
import type { ModalProps } from '@components/Modal/dto';
88
import Modal from '@components/Modal';
9+
import debounce from 'lodash/debounce';
910

1011
const OOTD: React.FC = () => {
1112
const [feeds, setFeeds] = useState<PostSummary[]>([]);
@@ -48,25 +49,29 @@ const OOTD: React.FC = () => {
4849
};
4950

5051
useEffect(() => {
51-
// Intersection Observer 설정
52+
if (reachedEnd && observerRef.current && loadMoreRef.current) {
53+
observerRef.current.unobserve(loadMoreRef.current);
54+
return; // 더 이상 옵저버 실행 안 함
55+
}
56+
5257
observerRef.current = new IntersectionObserver(
53-
(entries) => {
58+
debounce((entries) => {
5459
const target = entries[0];
55-
if (target.isIntersecting && !isFetching) {
60+
console.log('Intersection Observer:', target.isIntersecting);
61+
if (target.isIntersecting && !isFetching && !reachedEnd) {
5662
getPostList();
5763
}
58-
},
64+
}, 300), // 디바운스 적용
5965
{
60-
root: null, // viewport
61-
rootMargin: '0px',
62-
threshold: 1.0, // 요소가 100% 보여질 때 트리거
66+
root: null,
67+
rootMargin: '100px', // 미리 데이터 로드
68+
threshold: 0, // 요소가 조금이라도 보이면 트리거
6369
},
6470
);
6571

6672
if (loadMoreRef.current) {
6773
observerRef.current.observe(loadMoreRef.current);
6874
}
69-
7075
return () => {
7176
if (observerRef.current && loadMoreRef.current) {
7277
observerRef.current.unobserve(loadMoreRef.current);

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,11 @@
10681068
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
10691069
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==
10701070

1071+
"@types/lodash@^4.17.13":
1072+
version "4.17.13"
1073+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.13.tgz#786e2d67cfd95e32862143abe7463a7f90c300eb"
1074+
integrity sha512-lfx+dftrEZcdBPczf9d0Qv0x+j/rfNCMuC6OcfXmO8gkfeNAY88PgKUbvG56whcN23gc27yenwF6oJZXGFpYxg==
1075+
10711076
"@types/node@>=12.12.47", "@types/node@>=13.7.0":
10721077
version "22.10.2"
10731078
resolved "https://registry.yarnpkg.com/@types/node/-/node-22.10.2.tgz#a485426e6d1fdafc7b0d4c7b24e2c78182ddabb9"
@@ -1927,6 +1932,11 @@ lodash.merge@^4.6.2:
19271932
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
19281933
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
19291934

1935+
lodash@^4.17.21:
1936+
version "4.17.21"
1937+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
1938+
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
1939+
19301940
long@^5.0.0:
19311941
version "5.2.3"
19321942
resolved "https://registry.yarnpkg.com/long/-/long-5.2.3.tgz#a3ba97f3877cf1d778eccbcb048525ebb77499e1"

0 commit comments

Comments
 (0)