Skip to content

Commit

Permalink
FEAT : 무한스크롤 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
junvhui committed Feb 25, 2024
1 parent 7b70c08 commit f200a88
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 3 additions & 1 deletion backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ async function connectToMongoDB() {

connectToMongoDB();

const PAGE_SIZE = 3;

app.get('/article', async (req, res) => {
const pageNumber = req.query.pageNumber ? parseInt(req.query.pageNumber) : 1;
const PAGE_SIZE = 3;

try {
const articles = await articleCollection
.find({})
Expand Down
8 changes: 3 additions & 5 deletions src/MainPage/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ const Post = () => {
// 이적 기사 최신순 정렬

console.log(articleData);
console.log('article');

// Intersection Observer를 이용하여 무한 스크롤 감지
const [pageNumber, setPageNumber] = useState(1);
Expand All @@ -213,13 +212,12 @@ const Post = () => {
}
}, [inView]);

// axios를 이용하여 데이터를 가져옴
// 이전 코드와 동일하게 axios를 이용하여 데이터를 가져옴
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get(`/article?pageNumber=${pageNumber}`);
const newData = response.data.article.reverse();

const newData = response.data.article;
setArticleData((prevData) => [...prevData, ...newData]); // 기존 데이터와 새로운 데이터 결합
} catch (error) {
console.error('Error fetching user data:', error);
Expand Down Expand Up @@ -276,7 +274,7 @@ const Post = () => {
) : (
<EmptyContainer>기사가 없습니다.</EmptyContainer>
)}
<div ref={ref}></div>
<div ref={ref}>ㅤㅤㅤ</div>
</PostContent>
</PostWrap>
);
Expand Down
2 changes: 1 addition & 1 deletion src/MainPage/Test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const Post = () => {
const fetchData = async () => {
try {
const response = await axios.get(`/article?pageNumber=${pageNumber}`);
const newData = response.data.articles;
const newData = response.data.article;
setArticleData((prevData) => [...prevData, ...newData]); // 기존 데이터와 새로운 데이터 결합
} catch (error) {
console.error('Error fetching user data:', error);
Expand Down

0 comments on commit f200a88

Please sign in to comment.