중첩 레이아웃과 경로 그룹
중첩 레이아웃으로 애플리케이션을 구성하고, 더 나은 모듈화를 위해 경로 그룹으로 경로를 정리합니다.
중첩 레이아웃과 경로 그룹은(는) CoddyKit의 무료 Next.js 15 Fullstack Web Apps 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Next.js 15 Fullstack Web Apps 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Next.js 15 Fullstack Web Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Nested Layouts: Shared UI
Welcome! In this lesson, we'll explore two powerful App Router features: Nested Layouts and Route Groups.
Nested layouts allow you to share UI components (like headers, sidebars, or footers) across multiple pages within a specific segment of your application. This helps maintain consistency and reduces code duplication.
How Layouts Work
In Next.js 15 App Router, layouts are defined by creating a layout.tsx file inside a folder.
- A layout component receives a
childrenprop, which will be the content of the nested route segments (e.g.,page.tsxor other nested layouts). - Layouts are rendered hierarchically, meaning an outer layout wraps an inner layout.
The Root Layout
Every Next.js App Router project must have a Root Layout. This is the top-most layout.tsx file located directly in your app/ directory.
- It defines the essential
<html>and<body>tags. - It applies to all routes in your application.
Root Layout Code
Here's a basic example of the required root layout. Notice how it takes children as a prop.
import './globals.css';
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{children}
</body>
</html>
);
}Creating a Nested Layout
To create a nested layout, simply add another layout.tsx file inside a subfolder within your app/ directory.
For example, app/dashboard/layout.tsx would create a layout specific to all routes under /dashboard.
Nested Layout Code Example
This example shows a DashboardLayout that wraps its content, adding a navigation bar specific to the dashboard section. The DashboardPage will be rendered within this layout.
// app/dashboard/layout.tsx
export default function DashboardLayout({ children }) {
return (
<section>
<nav>
<h1>Dashboard Nav</h1>
</nav>
{children}
</section>
);
}
// app/dashboard/page.tsx
export default function DashboardPage() {
return (
<div>
<h2>Welcome to Dashboard</h2>
<p>This content is inside the DashboardLayout.</p>
</div>
);
}Introducing Route Groups
Route Groups are folders in your app/ directory that don't affect the URL path. They are primarily used for:
- Organizing routes into logical groups.
- Applying specific layouts to a subset of routes without changing the URL.
You define a route group by wrapping the folder name in parentheses, like (groupName).
Why Use Route Groups?
Route groups are incredibly useful for:
- Separate Layouts: Easily apply a unique layout to a specific group of pages (e.g., an
(auth)group with a login layout). - Project Organization: Keep related routes together for better maintainability.
- Excluding Segments: Prevent a folder name from appearing in the URL path.
Route Group Layout Example
Here, a (marketing) route group contains its own layout. Any page within this group (like /about) will use this layout, but the URL remains /about, not /marketing/about.
// app/(marketing)/layout.tsx
export default function MarketingLayout({ children }) {
return (
<>
<header>
<h1>Marketing Site</h1>
</header>
<main>
{children}
</main>
<footer>
<p>© 2024 Marketing</p>
</footer>
</>
);
}
// app/(marketing)/about/page.tsx
export default function AboutPage() {
return (
<div>
<h2>About Us</h2>
<p>Learn more about our company.</p>
</div>
);
}Layouts & Groups Check
Consider the following Next.js App Router structure:
app/
├── layout.tsx
├── dashboard/
│ ├── layout.tsx
│ └── settings/
│ └── page.tsx
└── (auth)/
└── login/
└── page.tsx
Which layout.tsx files would apply to the app/dashboard/settings/page.tsx route?
Recap: Layouts & Groups
You've learned about structuring your Next.js application using Nested Layouts and Route Groups!
- Nested Layouts (`layout.tsx`) allow you to share UI components across segments of your app, creating a consistent user experience.
- Route Groups (`(folder)`) help organize your file structure and apply specific layouts to groups of routes without affecting the URL path.
These features are crucial for building scalable and maintainable Next.js applications!
자주 묻는 질문
“중첩 레이아웃과 경로 그룹” 강의는 무료인가요?
네 — “중첩 레이아웃과 경로 그룹” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Next.js 15 Fullstack Web Apps 강의 전체를 잠금 해제할 수 있습니다. Next.js 15 Fullstack Web Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“중첩 레이아웃과 경로 그룹”에서 뭘 배우나요?
중첩 레이아웃으로 애플리케이션을 구성하고, 더 나은 모듈화를 위해 경로 그룹으로 경로를 정리합니다. 브라우저에서 직접 실행하는 실습 코드로 Next.js 15 Fullstack Web Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Next.js 15 Fullstack Web Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Next.js 15 Fullstack Web Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“중첩 레이아웃과 경로 그룹” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Next.js 15 Fullstack Web Apps 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Next.js 15 Fullstack Web Apps 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 동적 경로와 포괄 세그먼트
- 중첩 레이아웃과 경로 그룹
- 병렬 경로와 가로채기 경로
- 로딩 및 오류 UI 규칙