Nested layouts help organize pages by sharing common parts like headers or sidebars. They make your app easier to build and keep consistent.
Nested layouts in NextJS
export default function Layout({ children }) { return ( <html> <body> <header>Header content</header> {children} <footer>Footer content</footer> </body> </html> ) }
The children prop is where nested pages or layouts appear.
Each layout wraps its nested content, creating a tree of layouts.
app/layout.js export default function RootLayout({ children }) { return ( <html> <body> <header>Main header</header> {children} <footer>Main footer</footer> </body> </html> ) }
app/dashboard/layout.js export default function DashboardLayout({ children }) { return ( <div> <nav>Dashboard sidebar</nav> <main>{children}</main> </div> ) }
app/dashboard/page.js export default function DashboardPage() { return <p>Dashboard home content</p> }
This example shows a root layout with header and footer. Inside it, the dashboard layout adds a sidebar and main area. The dashboard page content appears inside the dashboard layout.
// app/layout.js export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header style={{ padding: '1rem', backgroundColor: '#eee' }}> <h1>My Site</h1> </header> {children} <footer style={{ padding: '1rem', backgroundColor: '#eee' }}> <p>Ā© 2024 My Site</p> </footer> </body> </html> ) } // app/dashboard/layout.js export default function DashboardLayout({ children }) { return ( <div style={{ display: 'flex' }}> <nav style={{ width: '200px', backgroundColor: '#ddd', padding: '1rem' }}> <ul> <li>Dashboard Home</li> <li>Settings</li> </ul> </nav> <main style={{ flex: 1, padding: '1rem' }}>{children}</main> </div> ) } // app/dashboard/page.js export default function DashboardPage() { return <p>Welcome to your dashboard!</p> }
Layouts in Next.js 13+ live in the app folder and wrap nested folders/pages automatically.
Use semantic HTML and ARIA roles inside layouts for better accessibility.
Test layouts by viewing nested pages in the browser and using DevTools to inspect structure.
Nested layouts let you share page parts like headers and sidebars easily.
They keep your app organized and consistent.
Next.js automatically combines layouts from parent folders down to pages.
Practice
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 CQuick Check:
Nested layouts = shared UI parts [OK]
- Confusing layouts with data fetching
- Thinking layouts handle authentication
- Believing layouts only affect server rendering
Solution
Step 1: Recall Next.js layout file naming
Layouts are defined in files namedlayout.jsinside folders.Step 2: Differentiate from other files
page.jsdefines pages,app.jsis not used in App Router, andindex.jsis a page entry.Final Answer:
layout.js -> Option DQuick Check:
Layout file = layout.js [OK]
- Using page.js instead of layout.js for layouts
- Confusing app.js with layout.js
- Thinking index.js defines layouts
/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?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 BQuick Check:
Layouts wrap children in folder order [OK]
- Mixing order of html and section tags
- Placing body inside section incorrectly
- Ignoring root layout wrapping
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?
Solution
Step 1: Check layout file naming
Next.js requires the layout file to be named exactlylayout.jsto 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 namedlayout.js-> Option AQuick Check:
Layout file name = layout.js [OK]
- Using wrong file name like Layout.js or layout.jsx
- Thinking layout can't use div wrapper
- Forgetting to export default in page
- 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 {children}
}
// /app/dashboard/page.js
export default function DashboardPage() {
return Dashboard Content
}Solution
Step 1: Understand layout nesting in Next.js
Layouts nest by folder structure: root layout wraps dashboard layout, which wraps the page.Step 2: Analyze the given code structure
The root layout adds a header inside . The dashboard layout adds a sidebar inside a div. The page renders main content.Step 3: Combine nesting results
The final render isHeader .Dashboard Content Final Answer:
The page content will be inside <main> inside <div> with sidebar, wrapped by <body> with header -> Option AQuick Check:
Layouts nest by folder, wrapping children [OK]
- Thinking layouts replace each other instead of nesting
- Assuming only one layout can be active
- Ignoring children prop in layouts
Ready to prove it?
Pass the test and this pattern moves unknown
