일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Python
- git
- NextJs
- 희망
- 클래스 속성
- Kotlin If
- 장고 가상환경
- 도전
- 좋은글
- 파이썬
- 파이썬 클래스
- github
- python Django
- Kotlin else if
- Kotlin 클래스 속성정의
- 파이썬 반복문
- 자바 기본타입
- Kotlin 클래스
- 강제 타입변환
- django virtualenv
- 파이썬 제어문
- 성공
- 다중조건문
- Variable declaration
- 넥스트js
- Kotlin 조건문
- activate 오류
- 파이썬 장고
- Python Class
- Kotlin Class
Archives
- Today
- Total
키모스토리
#6. Active Links 본문
반응형
1. 기존 layout.jsx 파일에 현재 url을 체크하여 스타일지정
"use client"
import "./globals.css";
import {Poppins} from "next/font/google";
import { usePathname } from "next/navigation";
import Link from "next/link";
const poppins = Poppins({
subsets: ["latin"],
weight: ["200", "400", "700"],
style: ["normal", "italic"],
variable: "--font-poppins",
});
export default function RootLayout({ children }) {
const pathname = usePathname();
// console.log(pathname);
return (
<html lang="en">
<body className={`${poppins.variable} font-sans`}>
<header>
<nav>
<Link className={`nav-link ${pathname==="/"? "nav-link-active":""}`} href="/">
Home
</Link>
<div>
<Link className={`nav-link ${pathname==="/register"? "nav-link-active":""}`} href="/register">
Register
</Link>
<Link className={`nav-link ${pathname==="/dashboard"? "nav-link-active":""}`} href="/dashboard">
Dashboard
</Link>
</div>
</nav>
</header>
<main>
{children}
</main>
<footer>
footer
</footer>
</body>
</html>
);
}
2. 반복되는 Link 코드 부분을 컴포넌트로 분리함.
/src/components/NavLink.jsx
"use client"
import Link from "next/link";
import { usePathname } from "next/navigation";
export default function NavLink({label, href}) {
const pathname = usePathname();
return (
<Link className={`nav-link ${pathname===href ? "nav-link-active":""}`} href={href}>
{label}
</Link>
)
}
3. layout.jsx 수정
import "./globals.css";
import {Poppins} from "next/font/google";
import NavLink from "@/components/NavLink";
const poppins = Poppins({
subsets: ["latin"],
weight: ["200", "400", "700"],
style: ["normal", "italic"],
variable: "--font-poppins",
});
export default function RootLayout({ children }) {
return (
<html lang="en">
<body className={`${poppins.variable} font-sans`}>
<header>
<nav>
<NavLink label="Home" href="/" />
<div>
<NavLink label="Register" href="/register" />
<NavLink label="Dashboard" href="/dashboard" />
</div>
</nav>
</header>
<main>
{children}
</main>
<footer>
footer
</footer>
</body>
</html>
);
}
4. nav 전체를 컴포넌트로 분리
/src/components/Navigation.jsx
"use client"
import NavLink from "./NavLink"
export default function Navigation() {
return (
<nav>
<NavLink label="Home" href="/" />
<div>
<NavLink label="Register" href="/register" />
<NavLink label="Dashboard" href="/dashboard" />
</div>
</nav>
)
}
5. Footer 컴포넌트 작성
export default function Footer() {
return (
<footer>
<p>
Copyright © {new Date().getFullYear()} - All right reserved by
LearnWithJon
</p>
<a
href="https://www.youtube.com/@LearnWithJonVadar"
target="_blank"
rel="external"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
className="fill-current"
>
<path d="M19.615 3.184c-3.604-.246-11.631-.245-15.23 0-3.897.266-4.356 2.62-4.385 8.816.029 6.185.484 8.549 4.385 8.816 3.6.245 11.626.246 15.23 0 3.897-.266 4.356-2.62 4.385-8.816-.029-6.185-.484-8.549-4.385-8.816zm-10.615 12.816v-8l8 3.993-8 4.007z"></path>
</svg>
</a>
</footer>
);
}
6. layout.jsx 수정
import "./globals.css";
import {Poppins} from "next/font/google";
import Navigation from "@/components/Navigation";
import Footer from "@/components/Footer";
const poppins = Poppins({
subsets: ["latin"],
weight: ["200", "400", "700"],
style: ["normal", "italic"],
variable: "--font-poppins",
});
export default function RootLayout({ children }) {
return (
<html lang="en">
<body className={`${poppins.variable} font-sans`}>
<header>
<Navigation />
</header>
<main>
{children}
</main>
<Footer />
</body>
</html>
);
}
반응형
'Web Devlopment > Next-Blog Project' 카테고리의 다른 글
#8. Auth & Guest Links (0) | 2025.04.05 |
---|---|
#7. Login (0) | 2025.04.05 |
#5. server-only (0) | 2025.04.05 |
#4. register (MongoDB, Jose, session, cookie) (0) | 2025.04.04 |
#3. zod.dev (validation) (0) | 2025.04.04 |