From b9e72885dd2ef2f2581ec8e581338f5f9f498eb3 Mon Sep 17 00:00:00 2001 From: dongree Date: Wed, 3 Jan 2024 23:00:20 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20useInfinityScroll=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useInfinityScroll.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/hooks/useInfinityScroll.ts b/src/hooks/useInfinityScroll.ts index 9b13cb8..f52903a 100644 --- a/src/hooks/useInfinityScroll.ts +++ b/src/hooks/useInfinityScroll.ts @@ -1,4 +1,4 @@ -import { Dispatch, SetStateAction, useEffect, useState } from 'react'; +import { Dispatch, SetStateAction, useState } from 'react'; import useObserver from '@/hooks/useObserver'; export const useInfinityScroll = ( @@ -24,20 +24,19 @@ export const useInfinityScroll = ( else { setData([...data, ...item]); } + }) + .then(() => { setIsLoading(false); + setPageNum((pre) => pre + 1); }) .catch(() => { setIsLoading(false); }); }; - useEffect(() => { - fetchData(); - }, [pageNum]); - const onIntersect: IntersectionObserverCallback = ([entry]) => { if (isEnd || isLoading) return; - entry.isIntersecting && setPageNum(pageNum + 1); + if (entry.isIntersecting) fetchData(); }; const { setTarget } = useObserver({ onIntersect });