📖 Scenario: You are building a simple website with a main layout and a nested dashboard layout inside it. The main layout has a header and footer. The dashboard layout adds a sidebar. You want to organize your pages so that the dashboard pages use both layouts automatically.
🎯 Goal: Create nested layouts in Next.js using the App Router. The main layout wraps all pages with a header and footer. The dashboard layout wraps dashboard pages with a sidebar inside the main layout.
📋 What You'll Learn
Create a main layout component with a header and footer
Create a dashboard layout component with a sidebar
Nest the dashboard layout inside the main layout
Create a dashboard page that uses the nested layouts
💡 Why This Matters
🌍 Real World
Nested layouts help organize complex websites by reusing common page parts like headers, footers, and sidebars.
💼 Career
Understanding nested layouts is important for building scalable React and Next.js applications used in professional web development.
Progress0 / 4 steps
1
Create the main layout
Create a file app/layout.tsx with a React functional component called RootLayout. It should return an HTML structure with <html>, <body>, a <header> containing the text "Main Header", and a <footer> containing the text "Main Footer". Use children as a prop to render nested content inside the <body> between the header and footer.
NextJS
Hint
Remember to create a React functional component named RootLayout that returns the HTML structure with header, children, and footer.
2
Create the dashboard layout
Inside the app/dashboard folder, create a file layout.tsx with a React functional component called DashboardLayout. It should accept children as a prop and return a <div> with a <nav> containing the text "Sidebar" and a <main> containing the children. This layout will be nested inside the main layout.
NextJS
Hint
Create a React functional component named DashboardLayout that returns a div with a nav and main containing children.
3
Create a dashboard page
Inside the app/dashboard folder, create a file page.tsx with a React functional component called DashboardPage. It should return a <section> with the text "Welcome to the Dashboard".
NextJS
Hint
Create a React functional component named DashboardPage that returns a section with the welcome text.
4
Complete nested layout usage
Verify that the DashboardPage is wrapped by DashboardLayout, which is nested inside the RootLayout. This happens automatically in Next.js App Router by placing layout.tsx files in app and app/dashboard. No extra code is needed here. Just ensure the files are correctly placed and exported.
NextJS
Hint
Make sure all three components are exported correctly and placed in the right folders for Next.js to nest layouts automatically.
Practice
(1/5)
1. What is the main purpose of nested layouts in Next.js?
easy
A. To handle user authentication
B. To load data from external APIs
C. To share common UI parts like headers and sidebars across pages
D. To manage server-side rendering only
Solution
Step 1: Understand what nested layouts do
Nested layouts allow you to reuse UI parts like headers and sidebars in multiple pages.
Step 2: Compare with other options
Loading data, authentication, and server-side rendering are separate concerns not handled by nested layouts.
Final Answer:
To share common UI parts like headers and sidebars across pages -> Option C
Quick Check:
Nested layouts = shared UI parts [OK]
Hint: Nested layouts share UI parts like headers [OK]
Common Mistakes:
Confusing layouts with data fetching
Thinking layouts handle authentication
Believing layouts only affect server rendering
2. Which file name is used to define a layout in Next.js 14+ nested layouts system?
easy
A. index.js
B. page.js
C. app.js
D. layout.js
Solution
Step 1: Recall Next.js layout file naming
Layouts are defined in files named layout.js inside folders.
Step 2: Differentiate from other files
page.js defines pages, app.js is not used in App Router, and index.js is a page entry.
Final Answer:
layout.js -> Option D
Quick Check:
Layout file = layout.js [OK]
Hint: Layouts use layout.js files, not page.js [OK]
Common Mistakes:
Using page.js instead of layout.js for layouts
Confusing app.js with layout.js
Thinking index.js defines layouts
3. Given this folder structure and files:
/app/layout.js
export default function RootLayout({ children }) {
return {children}
}
/app/dashboard/layout.js
export default function DashboardLayout({ children }) {
return {children}
}
/app/dashboard/page.js
export default function DashboardPage() {
return
Dashboard
}
What will be the rendered HTML structure when visiting /dashboard?
medium
A.
Dashboard
B.
Dashboard
C.
Dashboard
D.
Dashboard
Solution
Step 1: Understand layout nesting order
The root layout wraps the whole app with <html><body>. The dashboard layout wraps the dashboard page inside a <section>.
Step 2: Combine layouts and page output
The final output nests the dashboard page inside <section>, which is inside <body> and <html>.
Final Answer:
<html><body><section><h1>Dashboard</h1></section></body></html> -> Option B
Quick Check:
Layouts wrap children in folder order [OK]
Hint: Layouts wrap children top-down by folder nesting [OK]
Common Mistakes:
Mixing order of html and section tags
Placing body inside section incorrectly
Ignoring root layout wrapping
4. You have this layout file:
export default function Layout({ children }) {
return
{children}
}
But your page content does not appear inside the layout when you visit the page. What is the likely cause?
medium
A. The layout file is not named layout.js
B. The layout component must return a fragment, not a div
C. The page file is missing export default
D. The layout component must not accept children
Solution
Step 1: Check layout file naming
Next.js requires the layout file to be named exactly layout.js to apply nested layouts.
Step 2: Verify other options
Returning a div is valid, pages must export default, and layouts must accept children to render nested content.
Final Answer:
The layout file is not named layout.js -> Option A
Quick Check:
Layout file name = layout.js [OK]
Hint: Layout file must be named layout.js exactly [OK]
Common Mistakes:
Using wrong file name like Layout.js or layout.jsx
Thinking layout can't use div wrapper
Forgetting to export default in page
5. You want a nested layout structure where: - The root layout adds a header - The dashboard layout adds a sidebar - The dashboard page shows content
Which code correctly nests these layouts and page in Next.js 14+?
// /app/layout.js
export default function RootLayout({ children }) {
return Header{children}
}
// /app/dashboard/layout.js
export default function DashboardLayout({ children }) {
return