Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/pages/detail/ui/ReviewList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from '@emotion/styled';
import { useInfiniteQuery } from '@tanstack/react-query';
import { startTransition, useCallback, useEffect, useState } from 'react';
import { startTransition, useCallback, useEffect, useRef, useState } from 'react';
import { useParams } from 'react-router-dom';

import { ANCHOR_REVIEW } from 'pages/detail/config/anchor';
Expand Down Expand Up @@ -84,6 +84,24 @@
return () => window.removeEventListener('scroll', handleScroll);
}, [handleScroll]);

const loaderRef = useRef(null);

useEffect(() => {
if (!hasNextPage || isFetchingNextPage) return;
const observer = new window.IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
void fetchNextPage();
}
},
{ rootMargin: '200px' } // 미리 로드
);
if (loaderRef.current) observer.observe(loaderRef.current);
return () => {
if (loaderRef.current) observer.unobserve(loaderRef.current);

Check warning on line 101 in src/pages/detail/ui/ReviewList.tsx

View workflow job for this annotation

GitHub Actions / run lint, build

The ref value 'loaderRef.current' will likely have changed by the time this effect cleanup function runs. If this ref points to a node rendered by React, copy 'loaderRef.current' to a variable inside the effect, and use that variable in the cleanup function
};
}, [hasNextPage, isFetchingNextPage, fetchNextPage]);

if (isLoading) {
return (
<Section>
Expand All @@ -97,7 +115,7 @@
return (
<Section>
<SectionTitle>
별점 및 리뷰({allReviews.length})
별점 및 리뷰({data?.pages[0].data.totalElements})
<Filter
placeholder={selectedFilter.label}
options={REVIEW_FILTER_OPTIONS}
Expand All @@ -107,6 +125,7 @@
</SectionTitle>
<ReplyList id={ANCHOR_REVIEW}>
<Reply comments={allReviews} onDeleteReply={handleDeleteReply} onModifyReply={handleModifyReply} />
<div ref={loaderRef} />
{isFetchingNextPage && (
<LoadingContainer>
<Skeleton />
Expand Down
Loading