Nextjs Fundamentals · App Router

Nested layouts

nested layouts and route segments

Nested layouts

Layouts in the folder hierarchy nest by default. Each layout wraps the layouts and pages below it through its children prop, so shared chrome is composed layer by layer.

To add a layout for a route, drop a layout file inside that route segment's folder:

// app/blog/layout.tsx
export default function BlogLayout({ children }: { children: React.ReactNode }) {
  return <section>{children}</section>;
}

With the root layout in app/layout.tsx and this one in app/blog/layout.tsx, the root layout wraps the blog layout, which in turn wraps the pages beneath /blog — app/blog/page.tsx and the dynamic app/blog/[slug]/page.tsx:

app/
  layout.tsx          # root layout (wraps everything)
  blog/
    layout.tsx        # nested layout (wraps the /blog routes)
    page.tsx          # /blog
    [slug]/
      page.tsx        # /blog/[slug]

Because the root layout stays mounted as you move between /blog and a post, its state is preserved and it does not rerender. Each nested layout only needs to describe the chrome for its segment.

You are about to answer a short quiz on nested layouts and route segments.

  1. A layout in a nested route segment wraps the layouts and pages below it via `children`.
  2. To add a layout for the `/blog` route, add a `layout` file inside the ___ folder.
  3. A layout preserves its state across navigations that stay within it.
  4. Only the root layout can wrap another layout.
  5. The root layout wraps a nested layout, which in turn wraps the route's ___.