diff --git a/src/hooks/useChart.js b/src/hooks/useChart.js index fe767f7..a43a890 100644 --- a/src/hooks/useChart.js +++ b/src/hooks/useChart.js @@ -13,7 +13,7 @@ export const useChart = () => { const [idols, setIdols] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); - const [visibleCount, setVisibleCount] = useState(ITEMS_PER_PAGE); + const [visibleCount, setVisibleCount] = useState(ITEMS_PER_PAGE); // ✅ useState 유지 useEffect(() => { const fetchIdols = async () => { @@ -32,18 +32,22 @@ export const useChart = () => { fetchIdols(); }, []); + // ✅ 오직 최초 1회만 visibleCount 초기화 useEffect(() => { - const handleResize = () => { - if (getIsMobile()) setVisibleCount(MOBILE_ITEMS_PER_PAGE); - else if (getIsTablet()) setVisibleCount(TABLET_ITEMS_PER_PAGE); - else setVisibleCount(ITEMS_PER_PAGE); - }; - - handleResize(); - window.addEventListener("resize", handleResize); - return () => window.removeEventListener("resize", handleResize); + if (getIsMobile()) setVisibleCount(MOBILE_ITEMS_PER_PAGE); + else if (getIsTablet()) setVisibleCount(TABLET_ITEMS_PER_PAGE); + else setVisibleCount(ITEMS_PER_PAGE); }, []); + // ✅ 더보기 버튼 로직 + const handleMore = () => { + if (getIsMobile() || getIsTablet()) { + setVisibleCount((prev) => prev + 5); + } else { + setVisibleCount((prev) => prev + 10); + } + }; + const femaleIdols = useMemo(() => { return idols .filter((i) => i.gender === "female") @@ -65,5 +69,6 @@ export const useChart = () => { setIdols, visibleCount, setVisibleCount, + handleMore, // ✅ Chart.jsx에서 사용 }; }; diff --git a/src/pages/List/Chart/index.jsx b/src/pages/List/Chart/index.jsx index 9247861..812e090 100644 --- a/src/pages/List/Chart/index.jsx +++ b/src/pages/List/Chart/index.jsx @@ -44,6 +44,7 @@ const Chart = () => { setIdols, visibleCount, setVisibleCount, + handleMore, // ✅ useChart에서 내려받음 } = useChart(); const openModal = () => setIsModalOpen(true); @@ -55,18 +56,6 @@ const Chart = () => { visibleCount, ); - const handleMore = () => { - if (window.matchMedia("(max-width: 425px)").matches) { - setVisibleCount((prev) => prev + 5); - } else if ( - window.matchMedia("(min-width: 426px) and (max-width: 768px)").matches - ) { - setVisibleCount((prev) => prev + 5); - } else { - setVisibleCount((prev) => prev + 10); - } - }; - const handleIdolClick = (idol) => { const mockData = idolProfiles[idol.name]; if (mockData) {