Skip to content

Commit 25aacbc

Browse files
committed
Fix: shallow 라우팅 사용
1 parent d2c90c1 commit 25aacbc

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

components/Pagination.tsx

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import useViewport from "@/hooks/useViewport";
12
import Image from "next/image";
2-
import Link from "next/link";
33
import { useRouter } from "next/router";
44
import { useEffect, useState } from "react";
55

@@ -8,32 +8,36 @@ interface PaginationProps {
88
}
99

1010
const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
11-
console.log(totalCount);
1211
const router = useRouter();
1312
const LiStyle = "relative w-12 h-12 rounded-lg bg-gray900";
14-
const buttonStyle = "flex justify-center items-center h-full text-black400";
13+
const buttonStyle =
14+
"flex justify-center items-center w-full h-full text-black400";
1515
const { page, pageSize } = router.query;
1616

1717
const currentPage = Number(page) || 1;
1818
const currentPageSize = Number(pageSize) || 6;
1919
const totalPages = Math.ceil(totalCount / currentPageSize);
2020
const [maxPagesToShow, setMaxPagesToShow] = useState(2);
21+
const { isPC } = useViewport();
2122

22-
// 화면 크기 변화에 따라 pageSize와 maxPagesToShow를 설정
23-
useEffect(() => {
24-
const handleResize = () => {
25-
const width = window.innerWidth;
26-
setMaxPagesToShow(width > 1024 ? 5 : 3);
27-
};
28-
29-
// 초기 설정 및 리사이즈 이벤트 리스너 추가
30-
handleResize();
31-
window.addEventListener("resize", handleResize);
23+
const handlePageChange = (newPage: number) => {
24+
if (newPage !== currentPage) {
25+
router.push(
26+
`/link?page=${newPage}&pageSize=${currentPageSize}`,
27+
undefined,
28+
{ shallow: true }
29+
);
30+
}
31+
};
3232

33-
return () => {
34-
window.removeEventListener("resize", handleResize);
35-
};
36-
}, []);
33+
// 화면 크기 변화에 따라 maxPagesToShow를 설정
34+
useEffect(() => {
35+
if (isPC) {
36+
setMaxPagesToShow(5);
37+
} else {
38+
setMaxPagesToShow(3);
39+
}
40+
}, [isPC]);
3741

3842
// 페이지 리스트 생성 함수
3943
const getPageNumbers = () => {
@@ -60,9 +64,10 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
6064
return (
6165
<ul className="flex justify-center gap-[10px] my-10">
6266
<li className={LiStyle}>
63-
<Link
64-
href={`/link?page=${currentPage - 1}&pageSize=${currentPageSize}`}
67+
<button
68+
onClick={() => handlePageChange(currentPage - 1)}
6569
className={`${buttonStyle} ${currentPage > 1 ? "text-black500" : "pointer-events-none"}`}
70+
disabled={currentPage <= 1}
6671
>
6772
<Image
6873
src={
@@ -74,19 +79,19 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
7479
width={24}
7580
alt="prev"
7681
/>
77-
</Link>
82+
</button>
7883
</li>
7984

8085
{/* 페이지 번호와 생략 표시 */}
8186
{getPageNumbers().map((pageNum, index) =>
8287
typeof pageNum === "number" ? (
8388
<li key={index} className={LiStyle}>
84-
<Link
85-
href={`/link?page=${pageNum}&pageSize=${currentPageSize}`}
89+
<button
90+
onClick={() => handlePageChange(pageNum)}
8691
className={`${buttonStyle} ${pageNum === currentPage ? "text-black500" : "text-black400"}`}
8792
>
8893
{pageNum}
89-
</Link>
94+
</button>
9095
</li>
9196
) : (
9297
<li
@@ -99,9 +104,10 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
99104
)}
100105

101106
<li className={LiStyle}>
102-
<Link
103-
href={`/link?page=${currentPage + 1}&pageSize=${currentPageSize}`}
107+
<button
108+
onClick={() => handlePageChange(currentPage + 1)}
104109
className={`${buttonStyle} ${currentPage < totalPages ? "text-black500" : "pointer-events-none"}`}
110+
disabled={currentPage >= totalPages}
105111
>
106112
<Image
107113
src={
@@ -113,7 +119,7 @@ const Pagination: React.FC<PaginationProps> = ({ totalCount }) => {
113119
height={24}
114120
alt="next"
115121
/>
116-
</Link>
122+
</button>
117123
</li>
118124
</ul>
119125
);

hooks/useFetchLinks.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { useEffect } from "react";
1+
import { useEffect, useState } from "react";
22
import { proxy } from "@/lib/api/axiosInstanceApi";
33
import { LinkData } from "@/types/linkTypes";
4-
import useViewport from "./useViewport";
54

65
// 링크페이지의 query가 바뀌면 새로운 리스트로 업데이트 해주는 훅
76
const useFetchLinks = (
@@ -11,13 +10,11 @@ const useFetchLinks = (
1110
},
1211
setLinkCardList: (list: LinkData[]) => void
1312
) => {
14-
const { isTablet } = useViewport();
15-
1613
useEffect(() => {
1714
const fetchLinks = async () => {
1815
const res = await proxy.get("/api/links", {
1916
params: {
20-
page: query.page || 1,
17+
page: query.page,
2118
pageSize: 6,
2219
search: query.search,
2320
},
@@ -26,7 +23,7 @@ const useFetchLinks = (
2623
};
2724

2825
if (query) fetchLinks();
29-
}, [query, setLinkCardList, isTablet]);
26+
}, [query, setLinkCardList]);
3027
};
3128

3229
export default useFetchLinks;

pages/api/links/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import axiosInstance from "@/lib/api/axiosInstanceApi";
55
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
66
const cookies = parse(req.headers.cookie || "");
77
const accessToken = cookies.accessToken;
8-
const { page, pageSize, search } = req.query;
8+
const { page, pageSize = 6, search } = req.query;
99

1010
switch (req.method) {
1111
case "GET":

0 commit comments

Comments
 (0)