Pages and layouts

Beginner · 5 min

Pages render the UI for a URL, layouts wrap it and persist across navigation. The required root layout, nesting, templates, and the generated LayoutProps type.

Before thisThe file-system router

Codevolution · 5 min

Pages and layouts

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A layout wraps a segment and everything beneath it; it receives the child page or layout as `children`, and it keeps its state and does not re-render when you move between pages that share it.
  • The root layout at `app/layout.tsx` is required and must render the `<html>` and `<body>` tags.
  • Layouts nest: the root layout wraps a section layout, which wraps the page, so shared chrome such as a header and footer is defined once at the right level.
  • A `template` looks like a layout but remounts on navigation, which resets child state and re-runs effects.

The problem layouts solve

Every page on a site has the same header, footer and perhaps a sidebar. If each page renders its own, you copy markup around, and worse, the header is rebuilt from scratch on every navigation, throwing away anything stateful in it, such as a half-typed search box or an open menu.

A layout fixes both. It is UI that belongs to a segment and everything under it, rendered once, with the changing part passed in as children.

The root layout

Every App Router project has one at app/layout.tsx. It is required, and since nothing else renders the document, it must include <html> and <body>:

// app/layout.tsx
import type { Metadata } from "next";

export const metadata: Metadata = { title: "My site" };

export default function RootLayout({ children }: LayoutProps<"/">) {
  return (
    <html lang="en">
      <body>
        <header>My site</header>
        {children}
        <footer>© 2026</footer>
      </body>
    </html>
  );
}

children is whichever page, or deeper layout, matches the current URL. Visit /about and the about page appears between header and footer. Visit /blog and the blog page takes its place. The header and footer stay mounted.

LayoutProps<"/"> is a type helper Next.js generates for you. It is global, needs no import, and is created when you run next dev, next build or next typegen. It types children and any named slots. The generic takes the route the layout serves, so for a layout in a route group, where the folder name is not part of the URL, the easiest thing is to type children by hand: { children }: { children: React.ReactNode }. I hit exactly this when building the examples: LayoutProps<"/about"> on a group layout is a type error.

Nesting

Add a layout.tsx inside a folder and it wraps that segment’s pages, inside the layouts above it:

// app/blog/layout.tsx
export default function BlogLayout({ children }: LayoutProps<"/blog">) {
  return (
    <div>
      <nav>Posts | Authors</nav>
      {children}
    </div>
  );
}

For /blog/authors, the render order is the root layout, then the blog layout, then the page. The root header sits outside the blog nav, and each level adds its own chrome. Go from /blog to /blog/authors and the blog layout stays; go to /about and it unmounts, because /about is outside the blog segment.

Two limits are worth knowing now. A layout receives children but cannot pass data into them as props; if a child needs something, it fetches it, and Next.js deduplicates the repeated request. And because a layout does not re-render between its child pages, it is not the place to read something that changes per page.

Templates

A template.tsx has the same shape as a layout and sits between a layout and its children. The difference is a unique key: a template remounts when its segment changes, including a dynamic param. State inside it resets, DOM is recreated, effects re-run, and Suspense fallbacks show again on every navigation. Use one when you want that reset, such as an enter animation, a per-page view counter, or a form that should clear. Most of the time, use a layout.

What the video covers

Codevolution’s chapters cover creating a basic layout, understanding the root layout, adding a header and footer, and visualizing layouts in action. It shows the root layout wrapping pages and the header and footer persisting, which is this lesson’s core. The nested layouts follow-up is linked under deeper. It was recorded in Next.js 15, so it types layout props by hand rather than with the generated LayoutProps helper shown above, and that does not affect the concepts.

Where this fits

Layouts and pages are the two files every route is built from. Next, dynamic routes and params makes a segment match many URLs instead of one.

Check yourself

  1. 01Why does a layout not re-render when you move between two pages inside it?

    The layout belongs to the segment, not to either page. Navigation swaps only the `children` slot, so the layout's React tree, including any Client Component state inside it, stays mounted.

  2. 02What must the root layout contain, and what happens if you leave it out?

    It must render `<html>` and `<body>`, since no other file provides them. Without a root layout the app does not build; the root layout is required.

  3. 03When would you use a `template` instead of a `layout`?

    When you want state to reset on every navigation. A template remounts when its segment changes, so an input's contents are cleared, effects re-run, and a Suspense fallback shows again, none of which happens in a layout.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark