レイアウトとページコンポーネント
整理されたUIを実現するため、App Router構造内でネストされたレイアウトとページコンポーネントを作成する方法を習得します
「レイアウトとページコンポーネント」はCoddyKit上の無料Next.js 15 Fullstack (App Router + Server Actions)レッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNext.js 15 Fullstack (App Router + Server Actions)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Next.js 15 Fullstack (App Router + Server Actions)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
App Router UI Structure
The Next.js App Router organizes your application's user interface (UI) using special files: layout.js and page.js.
This structured approach helps you build consistent UIs that are easy to manage and scale across your mobile app.
What is a Layout?
A layout defines shared UI for a segment of routes. Think of it as a wrapper that surrounds your pages and other layouts.
- Layouts can contain common elements like navigation bars, footers, or sidebars.
- They don't re-render when navigating between pages within the same layout, improving performance.
The Root Layout File
Every App Router project must have a root layout file named app/layout.js at the top level of your app directory.
This layout defines the essential HTML structure, including the <html> and <body> tags, for your entire application.
Basic Root Layout Code
The children prop in a layout component is crucial. It represents the content of the nested layouts or pages below it.
This example shows a basic root layout with a header and footer.
app/layout.js
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>My App Header</header>
{children}
<footer>My App Footer</footer>
</body>
</html>
);
}What is a Page Component?
A page component is defined by a page.js file. This file contains the unique UI content specifically for a given route segment.
- Each URL path (e.g.,
/,/about) typically has its ownpage.js. - It renders within its parent layout(s), filling the
{children}slot.
Basic Page Component Code
This app/page.js file creates the UI for your root URL (/). It will be automatically wrapped by your app/layout.js.
app/page.js
export default function HomePage() {
return (
<main>
<h1>Welcome to CoddyKit!</h1>
<p>Start learning Next.js 15 today!</p>
</main>
);
}Creating Nested Layouts
You can create nested layouts by simply adding a layout.js file inside any subfolder within your app directory.
This allows you to apply shared UI to specific sections of your app, like a dashboard or user profile, while inheriting the root layout's structure.
Nested Layout Example
Here's how you'd create a nested layout for a /dashboard route. This dashboard layout wraps all pages within the /dashboard segment.
app/dashboard/layout.js
export default function DashboardLayout({ children }) {
return (
<section>
<nav>
<p>Dashboard Navigation</p>
</nav>
{children} {/* This will be app/dashboard/page.js */}
</section>
);
}
app/dashboard/page.js
export default function DashboardPage() {
return (
<h1>Dashboard Overview</h1>
);
}Layouts & Pages Hierarchy
The rendering order for your UI components flows from the outermost layout to the innermost page:
app/layout.js(Root Layout)app/dashboard/layout.js(Nested Layout, if applicable)app/dashboard/page.js(Specific Page Content)
Each layout receives the next level of UI as its children prop.
Quick Check
Test your knowledge on how Next.js App Router structures UI.
Recap: Layouts & Pages
You've learned how Next.js App Router uses layout.js files for shared UI and page.js files for unique content specific to a route.
Understanding this fundamental structure is key to building organized and scalable Next.js applications!
よくある質問
「レイアウトとページコンポーネント」レッスンは無料ですか?
はい。「レイアウトとページコンポーネント」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Next.js 15 Fullstack (App Router + Server Actions)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Next.js 15 Fullstack (App Router + Server Actions)コースには全4レッスンが含まれています。
「レイアウトとページコンポーネント」で何を学びますか?
整理されたUIを実現するため、App Router構造内でネストされたレイアウトとページコンポーネントを作成する方法を習得します ブラウザで直接実行するハンズオンコードでNext.js 15 Fullstack (App Router + Server Actions)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Next.js 15 Fullstack (App Router + Server Actions)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのNext.js 15 Fullstack (App Router + Server Actions)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「レイアウトとページコンポーネント」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このNext.js 15 Fullstack (App Router + Server Actions)レッスンでコードを書いて実行できますか?
はい。すべてのNext.js 15 Fullstack (App Router + Server Actions)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- レイアウトとページコンポーネント
- 動的ルートとパラメーター
- ローディングUIとエラーUI
- ルートグループとネストされたレイアウト