Skip to content

Commit

Permalink
FEAT : 무한 스크롤 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
junvhui committed Feb 24, 2024
1 parent 3667eca commit 34af1a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 1 addition & 3 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ 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
10 changes: 9 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ async function connectToMongoDB() {
connectToMongoDB();

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({}).toArray();
const articles = await articleCollection
.find({})
.sort({ _id: -1 }) // _id를 기준으로 역순으로 정렬
.skip((pageNumber - 1) * PAGE_SIZE)
.limit(PAGE_SIZE)
.toArray();

res.json({ result: 'success', article: articles });
} catch (error) {
console.error('Error fetching articles:', error);
Expand Down
2 changes: 1 addition & 1 deletion src/MainPage/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const Post = () => {
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get(`/article`);
const response = await axios.get(`/article?pageNumber=${pageNumber}`);
const newData = response.data.article;
setArticleData((prevData) => [...prevData, ...newData]); // 기존 데이터와 새로운 데이터 결합
} catch (error) {
Expand Down

0 comments on commit 34af1a7

Please sign in to comment.