Skip to the solution
EasyFrontend8 min read

What are layouts in Next.js and how do nested layouts work?

1.1k views

The problem

Question presented to candidate: "In the Next.js App Router, what is a layout, how do layouts nest, and what actually happens to a layout when the user clicks between two pages that share it?"

What a strong answer should cover:

  • A layout is a layout.tsx file whose component receives children and wraps every page in that folder and below it.
  • The root layout in app/ is required and must render <html> and <body>; nested layouts stack by folder (root > dashboard > settings > page).
  • On client navigation, shared layouts are kept: they do not re-render or remount, so their state (and Client Components inside them) survives.
  • Consequences: layouts cannot read searchParams and should not depend on the pathname; use a Client Component with usePathname or the page props instead. They cannot pass props to children.
  • template.tsx is the alternative that remounts on every navigation; route groups and multiple root layouts change which layout applies.

Clarifying questions expected:

  • "Should this UI keep its state between pages (a sidebar, a player) or reset (an animation, a form)?" β€” that is the layout versus template choice.
  • "Do different sections need entirely different shells?" β€” route groups or multiple root layouts.

Code / implementation expected: Yes β€” a root layout plus one nested layout with two pages.

  • nextjs
  • app-router
  • layouts

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

8 min read Β· 9 sections

Read the code

Root layout, a nested dashboard layout and its pages
// app/layout.tsx β€” root layout: required, wraps every route
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <header>My Shop</header>
        {children}
      </body>
    </html>
  );
}

// app/dashboard/layout.tsx β€” wraps /dashboard and everything below it
import DashboardNav from "./DashboardNav";

export default function DashboardLayout({ children }: LayoutProps<"/dashboard">) {
  return (
    <section style={{ display: "flex", gap: 24 }}>
      <DashboardNav />
      <main>{children}</main>
    </section>
  );
}

// app/dashboard/page.tsx β€” URL /dashboard
export default function DashboardHome() {
  return <h1>Overview</h1>;
}

// app/dashboard/settings/page.tsx β€” URL /dashboard/settings
// Rendered as: RootLayout > DashboardLayout > SettingsPage
export default function SettingsPage() {
  return <h1>Settings</h1>;
}
Active nav link in a layout (Client Component)
// app/dashboard/DashboardNav.tsx
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";

const links = [
  { href: "/dashboard", label: "Overview" },
  { href: "/dashboard/settings", label: "Settings" },
];

export default function DashboardNav() {
  // Layouts do not re-render on navigation, but this Client Component does,
  // so it always sees the current pathname.
  const pathname = usePathname();
  return (
    <nav>
      {links.map((l) => (
        <Link key={l.href} href={l.href} aria-current={pathname === l.href ? "page" : undefined}>
          {l.label}
        </Link>
      ))}
    </nav>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 2 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextEasy

How do environment variables work in Next.js (the NEXT_PUBLIC_ prefix)?

Continue