Fullstack CourseLearn by building
Back to week 3

Topic

Use layouts and pages

Definition

In the App Router, page.tsx defines route-specific content and layout.tsx defines shared UI that wraps a route segment and its descendants.

In simpler words

A page is the screen for one URL; a layout is the durable frame around related screens.

Choose the file that owns UI so navigation, providers, and page-specific data do not become tangled.

After this you can

  • Map page and layout files to a route
  • Choose page versus layout ownership
  • Use route groups to create separate shells

Compose shared frames

Layouts nest from the root down to the current segment. This makes them appropriate for shared navigation, application providers, and chrome that should survive a sibling navigation.

A page should own the content and data presentation specific to its route. Putting every page concern in a layout makes the shared frame difficult to reason about.

A layout wrapping a page

// app/course/layout.tsx (sketch)
export default function CourseLayout({ children }: { children: React.ReactNode }) {
  return <>{children}</>;
}

// app/course/[track]/page.tsx
export default function TrackPage() {
  return <TrackRoadmap />;
}

The navigation belongs to the shared shell; ticket content belongs to its page.

Mistake: fetching page-only data in a layout

// Wrong — every child route now depends on tickets
export default async function Layout() {
  const tickets = await getTickets();
  return <TicketList tickets={tickets} />;
}

// Right — fetch/render ticket-specific content in tickets/page.tsx

Layouts should not accidentally make sibling routes load unrelated data.

Keep providers intentional

Definition

A layout can provide stable context to its descendants, but a Client Component provider makes its subtree client-capable.

In simpler words

Place providers as low as their consumers allow.

Keep the root layout focused on document structure, global styles, and truly global providers.

Place interactive providers below server-rendered layout content when possible to preserve server boundaries.

Live playground

Layouts and pages playground

Move shared navigation between a page and layout, then predict which routes receive it.

layout.tsx (nav stays mounted)

page.tsx: Ticket list

Keep in mind

  • Put stable navigation in a layout.
  • Keep page-specific data and UI in page.tsx.
  • Use a route group when related pages need a different shell.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥80% (enforced by the API) to save progress.

Checking your session…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. How do nested layouts work?
Concept
2. Root layout requirement?
Syntax
3. children prop in layout?
Practical
4. When to add a segment layout?
Logic
5. Templates vs layouts?
Concept
6. Does a layout re-fetch on every child navigation always?
Practical
7. Multiple root layouts?
Syntax
8. Page as default export?
Logic
9. Passing data from layout to page?
Concept
10. Which is true?
Practical
11. What must the root layout include?
Conceptintermediate
export default function RootLayout({ children }) {
  return (<html><body>{children}</body></html>);
}
12. What does children represent here?
Syntaxintermediate
function DashboardLayout({ children }) {
  return (<><SideNav />{children}</>);
}
13. Why does layout state persist when navigating between child pages?
Practicaladvanced
// layout.tsx keeps a client counter across child navigations
14. How does a template differ from a layout?
Logicadvanced
// template.tsx instead of layout.tsx
export default function Template({ children }) {
  return <div>{children}</div>;
}
15. What is required of a page file?
Conceptintermediate
export default function Page() { return <h1>Tickets</h1>; }

Checking your session…