Next.js Pages & Layouts
Next.js layouts let you wrap pages with shared UI like headers, sidebars, and footers that stay visible as users move between routes. A layout renders once and preserves its state during navigation. Pages hold the unique content for each route.
This tutorial explains the difference between a page and a layout, how the root layout works, and how to nest layouts inside each other. You will build a root layout with a header and footer, then a dashboard layout with a sidebar shared by two pages. If you are new to routing, read Next.js App Router Basics first.
What Is the Difference Between a Page and a Layout?
A page is the unique content for one URL. A layout is shared UI that wraps one or more pages and persists across navigation. Pages change when the URL changes. Layouts stay mounted and keep their state .
app/
layout.tsx ← shared shell (header + footer)
page.tsx ← home page content
about/
page.tsx ← about page content (still inside the same shell)
How it works: The layout receives each page as a child. When you navigate from / to /about, the page content swaps but the layout stays in place. This is why a header with a search box keeps its typed text while you browse. The page does not contain the header. The layout does.
What Is the Root Layout and Why Is It Required?
The root layout is the top-level layout at app/layout.tsx. It is required in every App Router project. It must contain the <html> and <body> tags because no other file provides them .
// app/layout.tsx
import type { Metadata } from "next";
import "./globals.css";
export const metadata: Metadata = {
title: "My Next.js App",
description: "Learning pages and layouts",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>
<header>
<p>My Site</p>
</header>
{children}
<footer>
<p>© 2026 My Site</p>
</footer>
</body>
</html>
);
}
Browser shows on every page:
My Site ← header
(page content here)
© 2026 My Site ← footer
How it works: The children prop is where Next.js injects the active page. The header and footer sit outside {children}, so they appear on every route. The <html> and <body> tags belong here and nowhere else. If you delete this file, Next.js cannot render a valid HTML document.
How Is the children Prop Typed?
The children prop holds whatever Next.js renders inside the layout. In TypeScript strict mode, you type it as React.ReactNode, which accepts any renderable React content .
// app/layout.tsx (type annotation only)
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return <html lang="en"><body>{children}</body></html>;
}
How it works: React.ReactNode covers elements, strings, numbers, fragments, and arrays of these. The Readonly wrapper signals that the props object should not be mutated. You do not need to import React for this type in modern Next.js with the new JSX transform, but the type reference still works because React types are available globally through the project's TypeScript setup.
How Do Nested Layouts Work?
A nested layout sits inside a route folder and wraps only the pages below it. It adds extra shared UI on top of the root layout. The root layout still applies because layouts stack .
app/
layout.tsx ← root layout (header + footer)
dashboard/
layout.tsx ← dashboard layout (sidebar)
page.tsx → /dashboard
settings/
page.tsx → /dashboard/settings
// app/dashboard/layout.tsx
export default function DashboardLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<section>
<aside>
<nav>
<p>Dashboard Menu</p>
</nav>
</aside>
<div>{children}</div>
</section>
);
}
// app/dashboard/page.tsx
export default function DashboardHomePage() {
return <h1>Dashboard Overview</h1>;
}
// app/dashboard/settings/page.tsx
export default function SettingsPage() {
return <h1>Account Settings</h1>;
}
Browser shows at /dashboard:
My Site ← root header
Dashboard Menu ← dashboard sidebar
Dashboard Overview ← page content
© 2026 My Site ← root footer
Browser shows at /dashboard/settings:
My Site
Dashboard Menu
Account Settings
© 2026 My Site
How it works: The dashboard layout wraps both dashboard pages. The root layout wraps everything, including the dashboard layout. Next.js nests them automatically based on folder depth. The sidebar appears on both dashboard routes but not on the home page. Each layout keeps its own state during navigation.
Why Do Layouts Persist Across Navigation?
Layouts do not re-render when you move between pages they wrap. React keeps them mounted. This means interactive state inside a layout, like a search input or an open menu, survives navigation .
// app/layout.tsx (interactive header example)
"use client";
import { useState } from "react";
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const [query, setQuery] = useState<string>("");
return (
<html lang="en">
<body>
<header>
<input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder="Search"
/>
</header>
{children}
</body>
</html>
);
}
How it works: This example marks the layout as a Client Component because it uses useState. When you navigate to another page, the input keeps its typed text. The layout does not unmount. Pages inside it change, but the layout stays alive. This is one of the main reasons to move shared interactive UI into a layout.
When Should You Use template.tsx Instead of layout.tsx?
A template looks like a layout but behaves differently. It creates a new instance for each navigation. State is reset, effects run again, and the DOM is recreated. A layout persists. A template does not .
// app/template.tsx
export default function Template({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return <div>{children}</div>;
}
| Feature | layout.tsx | template.tsx |
|---|---|---|
| State on navigation | Preserved | Reset |
| Re-renders on route change | No | Yes |
| Use case | Headers, sidebars, persistent UI | Page-view analytics, enter animations |
How it works: Use a layout when shared UI should stay mounted. Use a template when you need code to run on every navigation, such as logging a page view or replaying an animation. Most projects need layouts far more often than templates.
Common Mistakes and Best Practices
- Putting
<html>or<body>in a nested layout. Only the root layout should contain these tags. Duplicating them in a nested layout produces invalid HTML and hydration errors. - Adding
"use client"to every layout. Layouts are Server Components by default and can fetch data. Add the directive only when the layout needs state, effects, or browser APIs. - Expecting a layout to re-run on navigation. Layouts persist by design. If you need fresh state on every route change, use a template or move the logic into the page.
- Fetching page-specific data in a layout. A layout wraps many pages and does not know which one is active. Fetch data in the page that needs it, not in the shared layout.
- Forgetting the
childrenprop. A layout that does not render{children}shows the header and footer but hides the page content. Always render the children.
Real-World Example: Dashboard with a Shared Shell and Sidebar
This example combines a root layout with header and footer, a dashboard layout with a sidebar, and two dashboard pages that share the sidebar.
app/
layout.tsx ← header + footer
dashboard/
layout.tsx ← sidebar
page.tsx → /dashboard
reports/
page.tsx → /dashboard/reports
// app/layout.tsx
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>
<header><h2>Analytics App</h2></header>
{children}
<footer><p>Built with Next.js</p></footer>
</body>
</html>
);
}
// app/dashboard/layout.tsx
export default function DashboardLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<section>
<aside>
<ul>
<li>Overview</li>
<li>Reports</li>
</ul>
</aside>
<main>{children}</main>
</section>
);
}
// app/dashboard/page.tsx
export default function DashboardPage() {
return <h1>Overview</h1>;
}
// app/dashboard/reports/page.tsx
export default function ReportsPage() {
return <h1>Reports</h1>;
}
Browser shows at /dashboard:
Analytics App ← root header
Overview ← sidebar items
Reports
Overview ← page content
Built with Next.js ← root footer
Line-by-line explanation: The root layout provides the page shell: header at the top, footer at the bottom, and {children} in between. The dashboard layout adds a sidebar beside {children}. When you visit /dashboard, Next.js renders the root layout, then the dashboard layout inside it, then the dashboard page inside that. When you visit /dashboard/reports, the same two layouts apply and only the page changes. Both dashboard pages share the sidebar without repeating code.
Summary
- A page holds unique content for one URL. A layout holds shared UI that persists across navigation.
- The root layout at
app/layout.tsxis required and must contain<html>and<body>tags. - Type the
childrenprop asReact.ReactNodeso any renderable React content is accepted. - Nested layouts stack on top of the root layout. A dashboard layout can add a sidebar without repeating the header and footer.
- Next.js layouts keep their state during navigation. Use
template.tsxwhen you need state to reset on every route change. - Use
template.tsxfor analytics or animations, and layouts for persistent shared UI.
Now that you can share UI across routes, continue to Next.js Navigation & Link to learn how to move between pages with the Link component and active link styles.
Frequently Asked Questions
Can a layout fetch data from a database?
Yes. Layouts are Server Components by default, so they can use async and await to fetch data. However, a layout wraps many pages and does not know which page is active. Fetch data that every page needs, like the logged-in user, in the layout. Fetch page-specific data in the page itself.
What happens if I do not render children in a layout?
The page content disappears. The layout still renders its header, sidebar, or footer, but the URL shows an empty area where the page should be. Always include {children} inside the layout's JSX. This is one of the most common beginner mistakes with Next.js layouts.
Can I have more than one root layout?
Not in the same route tree. The app/layout.tsx file is the single root layout for the whole application. You can use route groups to create separate root layouts for different sections, but that is an advanced pattern. For most projects, one root layout is enough.
Does a layout re-render when I navigate between its pages?
No. Layouts stay mounted during navigation, so React preserves their state. This is useful for search boxes and open menus. If you need a component to reset on every navigation, use template.tsx instead of layout.tsx. Templates recreate their DOM on each route change.
Where should I put a shared header and footer?
Put them in the root layout at app/layout.tsx. That way they appear on every page without repeating code. If only a section of the site needs a different header, add a nested layout inside that section's folder. The nested layout can add or replace UI on top of the root layout.
Practice Exercises
Easy: Add a Footer to the Root Layout
Modify app/layout.tsx to include a footer with the text Made with Next.js below the children.
Show solution
// app/layout.tsx
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body>
{children}
<footer><p>Made with Next.js</p></footer>
</body>
</html>
);
}
The footer appears on every page because it lives in the root layout.
Easy: Create a Nested Layout
Create a layout at app/blog/layout.tsx that adds a heading Blog Section above the children. Confirm the heading does not appear on the home page.
Show solution
// app/blog/layout.tsx
export default function BlogLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<section>
<h2>Blog Section</h2>
{children}
</section>
);
}
The blog layout applies only to routes under /blog, not to the home page.
Medium: Add a Template for Analytics
Create app/template.tsx that logs a message to the console on every navigation using useEffect. Explain why a layout cannot do this.
Show solution
// app/template.tsx
"use client";
import { useEffect } from "react";
export default function Template({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
useEffect(() => {
console.log("Page navigated");
}, []);
return <div>{children}</div>;
}
A layout stays mounted, so useEffect runs only once. A template remounts on each navigation, so the effect runs every time.
Challenge: Build a Dashboard with a Shared Sidebar
Create a dashboard layout with a sidebar containing two links (as plain text for now). Add two dashboard pages: /dashboard and /dashboard/analytics. Both pages should show the sidebar.
Show solution
app/
dashboard/
layout.tsx
page.tsx → /dashboard
analytics/
page.tsx → /dashboard/analytics
// app/dashboard/layout.tsx
export default function DashboardLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<section>
<aside>
<p>Overview</p>
<p>Analytics</p>
</aside>
<main>{children}</main>
</section>
);
}
// app/dashboard/page.tsx
export default function DashboardPage() {
return <h1>Dashboard Home</h1>;
}
// app/dashboard/analytics/page.tsx
export default function AnalyticsPage() {
return <h1>Analytics</h1>;
}
Both dashboard routes share the sidebar because the layout wraps every page in the folder.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts.
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
