Skip to content

Commit

Permalink
Fix: useInfinityScroll 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
dongree committed Jan 3, 2024
1 parent 999ebd5 commit f004989
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/hooks/useInfinityScroll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react';
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
import useObserver from '@/hooks/useObserver';

export const useInfinityScroll = <T>(
Expand All @@ -13,31 +13,26 @@ export const useInfinityScroll = <T>(
const [pageNum, setPageNum] = useState(1);
const [isEnd, setIsEnd] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const isInitialMount = useRef(true);

const fetchData = () => {
if (isLoading || isEnd) return;
setIsLoading(true);

fn(pageNum)
.then((item) => {
setIsLoading(false);
if (!item.length) setIsEnd(true);
else {
setData([...data, ...item]);
}
setIsLoading(false);
})
.catch(() => {
setIsLoading(false);
});
};

useEffect(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
} else {
fetchData();
}
fetchData();
}, [pageNum]);

const onIntersect: IntersectionObserverCallback = ([entry]) => {
Expand Down

0 comments on commit f004989

Please sign in to comment.