0
0
NextJSframework~10 mins

Nested layouts in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nested layouts
Start App
Root Layout Render
Nested Layout 1 Render
Nested Layout 2 Render
Page Component Render
Display Full Page with Nested Layouts
User Navigates -> Next Page
Reuse Layouts or Render New Nested Layouts
Update Page Content
Render Updated View
The app starts by rendering the root layout, then nested layouts inside it, and finally the page component. When navigating, layouts can be reused or new nested layouts rendered, updating the page content.
Execution Sample
NextJS
export default function RootLayout({ children }) {
  return <html><body>{children}</body></html>;
}

export function DashboardLayout({ children }) {
  return <section>{children}</section>;
}

export default function Page() {
  return <h1>Dashboard Home</h1>;
}
This code shows a root layout wrapping a dashboard layout, which wraps a page component displaying a heading.
Execution Table
StepComponent RenderedChildren PropOutput RenderedNotes
1RootLayoutDashboardLayout<html><body><section><h1>Dashboard Home</h1></section></body></html>RootLayout wraps all content
2DashboardLayoutPage<section><h1>Dashboard Home</h1></section>DashboardLayout wraps page content
3PageNone<h1>Dashboard Home</h1>Page renders heading
4User navigates to /dashboard/settingsDashboardLayout<html><body><section><h1>Settings</h1></section></body></html>Page changes, layouts reused
5DashboardLayoutSettingsPage<section><h1>Settings</h1></section>DashboardLayout wraps new page
6SettingsPageNone<h1>Settings</h1>Settings page renders heading
7User navigates to /profileProfileLayout<html><body><main><h1>Profile</h1></main></body></html>New nested layout used
8ProfileLayoutProfilePage<main><h1>Profile</h1></main>ProfileLayout wraps profile page
9ProfilePageNone<h1>Profile</h1>Profile page renders heading
10EndNoneRendering completeNo more navigation
💡 Rendering stops when user stops navigating or closes the app.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7Final
childrenundefinedDashboardLayout componentDashboardLayout with SettingsPageProfileLayout with ProfilePageProfileLayout with ProfilePage
currentPageundefinedDashboard HomeSettingsProfileProfile
Key Moments - 3 Insights
Why does the RootLayout render only once even when navigating between pages?
Because RootLayout is the top-level layout wrapping all pages, it stays mounted and only the nested layouts and pages inside it update, as shown in execution_table rows 1, 4, and 7.
How do nested layouts pass content down to pages?
Nested layouts receive their child content as the 'children' prop and render it inside their JSX, demonstrated in execution_table rows 2 and 5 where DashboardLayout wraps the page components.
What happens when navigating to a page with a different nested layout?
Next.js renders the new nested layout and its page inside the RootLayout, replacing the previous nested layout, as seen in execution_table rows 7-9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'children' prop of DashboardLayout at Step 4?
ARootLayout component
BSettingsPage component
CDashboardLayout component
DProfilePage component
💡 Hint
Check the 'Children Prop' column at Step 4 in the execution_table.
At which step does the app render a new nested layout different from DashboardLayout?
AStep 7
BStep 4
CStep 2
DStep 9
💡 Hint
Look for when ProfileLayout appears in the 'Component Rendered' column.
If the RootLayout was removed, what would happen to the rendered output at Step 1?
ADashboardLayout would wrap RootLayout
BThe page would not render at all
CThe page would render without <html> and <body> tags
DThe page would render twice
💡 Hint
RootLayout provides the and tags as shown in the output rendered at Step 1.
Concept Snapshot
Nested layouts in Next.js wrap page components inside each other.
RootLayout is the top wrapper, nested layouts go inside it.
Each layout receives 'children' prop to render inner content.
Layouts stay mounted across navigation if reused.
New nested layouts replace old ones when route changes.
This creates consistent page structure and UI reuse.
Full Transcript
Nested layouts in Next.js work by wrapping page components inside multiple layout components. The app starts rendering from the RootLayout, which wraps nested layouts like DashboardLayout or ProfileLayout. Each layout receives its inner content as the 'children' prop and renders it inside its JSX. When the user navigates, Next.js reuses layouts if possible or renders new nested layouts for different routes. This keeps the page structure consistent and allows UI parts like headers or sidebars to stay visible while only the page content changes. The execution table shows step-by-step how layouts and pages render and update during navigation, tracking the 'children' prop and current page content. Key moments clarify why RootLayout renders once, how children pass through layouts, and what happens when switching nested layouts. The visual quiz tests understanding of these steps and props. Overall, nested layouts help organize and reuse UI in Next.js apps efficiently.