Skip to content
RoutingChapter 5 / 34Beginner4 min read

Layouts & Templates

Next.js layouts explained: build shared UI with layout.tsx and template.tsx, the root layout, nested layouts, the LayoutProps helper, and how state is preserved.

On this page

1. What are layouts and templates?

A navbar, sidebar and footer are needed on every page. Instead of copying them into each page, you create a layout that renders the page inside itself through children.

2. Why does it matter?

Without a layoutWith a layout
The navbar is copied into every pageThe navbar lives in one place, on every page
The sidebar re-renders on navigationThe layout stays in place, state is safe
The sidebar's scroll and inputs resetState is preserved
Every page fetches the shared dataThe layout fetches it once

Next.js 16 also has layout deduplication. If 50 links share the same layout, that layout is downloaded only once during prefetch.

3. Where is it used?

Use caseFile
The shell of the whole site (html, body, fonts)app/layout.tsx
Dashboard sidebarapp/dashboard/layout.tsx
Blog sidebar and categoriesapp/blog/layout.tsx
An enter animation on every pageapp/template.tsx
Page view logging on every navigationAn effect inside template.tsx

4. How does it work?

Layouts nest. The root layout wraps everything, the segment's layout sits inside it, and the page sits innermost.

The render tree of /dashboard/settings
app/layout.tsx (html, body)
app/dashboard/layout.tsx (sidebar)
app/dashboard/settings/page.tsx

When the user goes from /dashboard/settings to /dashboard/billing, only the page changes. The dashboard layout stays the same, and so does its state.

layout.tsxtemplate.tsx
The same instance across navigationA new instance on every navigation
State is preservedState is reset
Effects do not run againEffects run every time
For most casesOnly when you need a reset

5. Code example

Root layout

tsx
import "./globals.css";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        <header>ZnShop</header>
        {children}
        <footer>Copyright ZnShop</footer>
      </body>
    </html>
  );
}

Nested layout with LayoutProps

tsx
import Link from "next/link";

export default function DashboardLayout({ children }: LayoutProps<"/dashboard">) {
  return (
    <div className="flex">
      <aside>
        <Link href="/dashboard">Overview</Link>
        <Link href="/dashboard/settings">Settings</Link>
      </aside>
      <section>{children}</section>
    </div>
  );
}

Template

tsx
export default function Template({ children }: { children: React.ReactNode }) {
  return <div className="fade-in">{children}</div>;
}

How does the code work?

PartMeaning
RootLayoutRequired in every app, html and body live only here
childrenThe inner layout or page
LayoutProps<"/dashboard">A global type helper, the type of children and params
app/dashboard/layout.tsxApplies only to /dashboard and the routes inside it
template.tsxRemounts on every navigation, so the animation plays again

Interview tip

"What is the difference between a layout and a template?" The answer: a layout persists across navigation and keeps its state, while a template mounts fresh on every navigation, so its state and effects reset.

6. Common mistakes

Mistake: Writing html and body in a nested layout too

Why? html and body should be only in the root layout, otherwise you get invalid HTML and hydration errors.

Better: In nested layouts keep only wrapper UI like a div or section.

Mistake: Trying to read searchParams in a layout

Why? A layout does not re-render on navigation, so it never receives the searchParams prop.

Better: Read searchParams in the page, or use useSearchParams in a Client Component.

Mistake: Using template.tsx everywhere

Why? Remounting on every navigation loses state and also does more work.

Better: Use a layout by default, and a template only when you need a reset or an enter animation.

7. What to learn before and after

Final quick revision

PointDetail
LayoutShared UI, persists across navigation
Root layoutRequired, with html and body
NestingLayouts nest according to folders
TemplateRemounts on every navigation
Type helperLayoutProps<"/route">
Next.js 16A shared layout is downloaded once during prefetch