Skip to content

Commit b157255

Browse files
committed
refactor(memory): use useRef for fetchMemories to ensure callback stability in infinite scrolling hooks
1 parent 655838b commit b157255

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/components/MemoriesPage.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ const MemoriesPage: React.FC<MemoriesPageProps> = ({ title, memoryType, requireA
2121
const { isAuthenticated } = useAuth(requireAuth);
2222

2323
// Only fetch memories if authentication is not required or user is authenticated
24-
const { memories, loading, hasMore, memoriesRef, fetchMemories, isInitialLoad } = useMemories({
24+
const { memories, loading, hasMore, memoriesRef, fetchMemories, isInitialLoad } = useMemories({
2525
memoryType,
2626
skipFetch: requireAuth && !isAuthenticated
2727
});
2828

29+
const fetchMemoriesRef = useRef(fetchMemories);
30+
2931
const handleCreateMemory = () => {
3032
navigate('/create-memory');
3133
};
3234

35+
// fetchMemories ref 업데이트
36+
useEffect(() => {
37+
fetchMemoriesRef.current = fetchMemories;
38+
}, [fetchMemories]);
39+
3340
// Setup intersection observer for infinite scrolling
3441
useEffect(() => {
3542
// 초기 로드 중이거나 로딩 중이면 observer 설정하지 않음
@@ -40,7 +47,7 @@ const MemoriesPage: React.FC<MemoriesPageProps> = ({ title, memoryType, requireA
4047
if (entries[0].isIntersecting && hasMore && !loading) {
4148
const currentMemories = memoriesRef.current;
4249
const lastMemoryId = currentMemories.length > 0 ? currentMemories[currentMemories.length - 1].id : undefined;
43-
fetchMemories(lastMemoryId);
50+
fetchMemoriesRef.current(lastMemoryId);
4451
}
4552
},
4653
{ threshold: 0.9 } // Trigger when 90% of the element is visible
@@ -57,7 +64,7 @@ const MemoriesPage: React.FC<MemoriesPageProps> = ({ title, memoryType, requireA
5764
observerRef.current.unobserve(loadingRef.current);
5865
}
5966
};
60-
}, [isInitialLoad, hasMore, loading, fetchMemories]);
67+
}, [isInitialLoad, hasMore, loading]);
6168

6269
return (
6370
<Container maxW="container.lg" centerContent flex="1" py={8}>

src/components/PublicMemoriesPage.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ const PublicMemoriesPage: React.FC<PublicMemoriesPageProps> = ({ title, requireA
2424
skipFetch: requireAuth && !isAuthenticated
2525
});
2626

27+
const fetchMemoriesRef = useRef(fetchMemories);
28+
2729
const handleCreateMemory = () => {
2830
navigate('/create-memory');
2931
};
3032

33+
// fetchMemories ref 업데이트
34+
useEffect(() => {
35+
fetchMemoriesRef.current = fetchMemories;
36+
}, [fetchMemories]);
37+
3138
// Setup intersection observer for infinite scrolling
3239
useEffect(() => {
3340
// 초기 로드 중이거나 로딩 중이면 observer 설정하지 않음
@@ -38,7 +45,7 @@ const PublicMemoriesPage: React.FC<PublicMemoriesPageProps> = ({ title, requireA
3845
if (entries[0].isIntersecting && hasMore && !loading) {
3946
const currentMemories = memoriesRef.current;
4047
const lastMemoryId = currentMemories.length > 0 ? currentMemories[currentMemories.length - 1].id : undefined;
41-
fetchMemories(lastMemoryId);
48+
fetchMemoriesRef.current(lastMemoryId);
4249
}
4350
},
4451
{ threshold: 0.9 } // Trigger when 90% of the element is visible
@@ -55,7 +62,7 @@ const PublicMemoriesPage: React.FC<PublicMemoriesPageProps> = ({ title, requireA
5562
observerRef.current.unobserve(loadingRef.current);
5663
}
5764
};
58-
}, [isInitialLoad, hasMore, loading, fetchMemories]);
65+
}, [isInitialLoad, hasMore, loading]);
5966

6067
return (
6168
<Container maxW="container.lg" centerContent flex="1" py={8}>

0 commit comments

Comments
 (0)