Skip to content

Commit

Permalink
Merge pull request #96 from Funssion-SWM/develop
Browse files Browse the repository at this point in the history
Fix: useInfinityScroll 수정
  • Loading branch information
dongree authored Jan 3, 2024
2 parents 866fbd8 + f004989 commit 67a5b71
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 67a5b71

Please sign in to comment.