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 });