SoFunction
Updated on 2025-04-13

Detailed explanation of the routing layout mechanism

Here I amApp Router, layout file () is nested layer by layer, each layer oflayoutCan inherit the previous layerlayout, and passchildrenTo render the content of the next layer. This mechanism allows the content between the page and the layout to be naturally recursively nested, forming a clear hierarchy.

Personal summary: When accessing /dashboard, inherit the parent layout, and children are in its own directory.

Example directory structure

Suppose there is the following project directory structure:

app/
          // Global layout            // Root page  dashboard/
          // Layout of Dashboard module            // Dashboard page    settings/
          // Dashboard Settings module layout            // Dashboard Settings page

Here are three layers:

  • The first layer is the global layout and pages ( + )。
  • The second layer is the Dashboard module (dashboard/ + dashboard/)。
  • The third layer is the Dashboard Settings submodule (dashboard/settings/ + dashboard/settings/)。

Implementation of various layers of code

1. Level 1: Global

// app/
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <header>Global Header</header>
        <main>{children}</main>
        <footer>Global Footer</footer>
      </body>
    </html>
  );
}

Function: Wrap all sub-contents and provide global header and Footer.

2. Level 2: Dashboard’s

// app/dashboard/
export default function DashboardLayout({ children }) {
  return (
    <div>
      <nav>Dashboard Sidebar</nav>
      <div>{children}</div>
    </div>
  );
}

Function: Add Dashboard's unique layout, such as the side navigation bar, to render its child content.

3. The third layer: Dashboard Settings’

// app/dashboard/settings/
export default function SettingsLayout({ children }) {
  return (
    <div>
      <h2>Settings Section</h2>
      <div>{children}</div>
    </div>
  );
}

Function: For Settings submodule, add independent layout parts, such as module title.

4. Page content example

Root page app/:

export default function HomePage() {
  return <h1>Welcome to the Home Page</h1>;
}

Dashboard page app/dashboard/:

export default function DashboardPage() {
  return <h1>Dashboard Overview</h1>;
}

Dashboard Settings Page app/dashboard/settings/:

export default function SettingsPage() {
  return <h1>Settings Content</h1>;
}

Rendering process and inheritance effect

When accessing /dashboard/settings, the page content will be nested layer by layer:

  • The first layer RootLayout rendering:
    • Provides global header and Footer.
    • Wrap the next layer through {children} (Dashboard ).
  • The second layer DashboardLayout rendering:
    • Render the sidebar of the Dashboard in the global layout.
    • Use {children} to wrap the next layer (Settings).
  • The third layer SettingsLayout rendering:
    • Add Settings' unique title to the Dashboard layout.
    • Use {children} to render the final page content.
  • Page content SettingsPage Rendering:
    • Shows the core content of the Settings page.

Final result

Visit/dashboard/settingsThe complete HTML rendering structure is as follows:

<html lang="en">
  <body>
    <header>Global Header</header>
    <main>
      <div>
        <nav>Dashboard Sidebar</nav>
        <div>
          <h2>Settings Section</h2>
          <div>
            <h1>Settings Content</h1>
          </div>
        </div>
      </div>
    </main>
    <footer>Global Footer</footer>
  </body>
</html>

Summarize

  • Inheritance layer by layer: Each floorWill passchildrenAutomatically receive and render the content of the next layer.
  • Flexible design: Each layer can add its own unique UI elements, while inheriting the layout of the previous layer.
  • Automated management: The App Router automatically handles layout nesting logic without manually passing layout content.

This is the end of this article about the detailed explanation of the routing layout mechanism. For more related routing layout content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!