+1 (843) 212-6898+8801897661858
Whatsapp-colorCreated with Sketch.
sales@mediusware.comSchedule A Call
Logo
Company
Services
Hire Developers
Industries
Case StudySTART FREE TRIALicon

About

Who We Are

Our Team

Blogs

Women Empowerment

Career

CSR

Delivery Models

Engagement Model

Services

Software Development

Web Development

Mobile App Development

E-commerce Development

Software Development

Enterprise Solutions

UI/UX Design

API Integration

Technology

React.js

Vue.js

Angular js

Laravel

Android

Flutter

iOS

React Native

Hire Developers

Hire Mobile App Developers

Hire Front-end Developers

Hire Backend Developers

Hire E-commerce Developers

Hire Developers

Hire Android Developers

Hire iOS Developers

Hire Swift Developers

Hire Flutter Developers

Hire React Native Developers

Hire Django Developers

Hire Node.js Developers

Hire Laravel Developers

We shape the art of technology
Headquarter

Headquarter

1050 Johnnie Dodds Blvd Mount Pleasant South Carolina USA ,Zip- 29464

sales@mediusware.io

+1 843-212-7149

Bangladesh Office

Bangladesh Office

24/1, Taj Mahal Road, Shiya Masjid mor, Floor - 8th & 9th, Ring Road, 1207, Dhaka, Bangladesh

sales@mediusware.com

+8801897661858

© 2025 Mediusware. All Rights Reserved

Terms & ConditionsPrivacy Policy

Table of contents

  1. Introduction
  2. What is a Root Layout?
  3. Implementation of Multiple Root Layout
  4. Testing the Layouts
  5. Conclusion
Share This Blog !
Get the best of our content straight to your inbox!

Don’t worry, we don’t spam!

Transform Your Next.js Experience: A Guide to Multiple Root Layouts in Next.js

Transform Your Next.js Experience: A Guide to Multiple Root Layouts in Next.js image

Last Update: 15 Oct 2024

Introduction

When building complex applications in Next.js, it's common to have different layouts for different sections of your app. With the introduction of the new App Router in Next.js 14, creating and managing multiple root layouts has become more flexible and intuitive. This guide will show you how to set up multiple root layouts in your Next.js application using the App Router.

What is a Root Layout?

A root layout in Next.js is a special layout that wraps around your page components, providing consistent UI elements like headers, footers, sidebars, or other shared elements. The App Router makes it easy to define these layouts in a file-based structure, ensuring that your layout logic is organized and easy to manage

Implementation of Multiple Root Layout

Step 1: Setting Up the Project

If you haven't already set up a Next.js project, you can do so by running the following command:

npx create-next-app@latest my-next-app
cd my-next-app

This will create a new Next.js project with the default configuration.

 

Step 2: Understanding the App Router Structure

Next.js 13 introduces the app/ directory as the new standard for building applications. In this directory, each route can have its own layout. You can create a folder structure to manage multiple root layouts based on the routes.

Here's an example of a typical structure when using the App Router with multiple layouts:

Fig: Folder Structure

 

  • dashboard/layout.js – Layout specific to the dashboard section.
  • auth/layout.js – Layout specific to the authentication section.
  • layout.js – The global root layout that wraps all other layouts.

 

Step 3: Creating Global Layout

The global root layout file provides a base layout that wraps around all pages in the application. To create this file, navigate to the app directory and add a layout.js file with the following code:

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

This layout will be used across your entire application unless overridden by a specific layout in a sub-directory.

 

Step 4: Creating a Specific Layout for the Dashboard

Now, let's create a specific layout for the dashboard section of your app. Inside the app/dashboard/ directory, create a layout.js file with the following content:

// app/dashboard/layout.js
export default function DashboardLayout({ children }) {
  return (
    <div>
      <aside>
        <nav>
          <ul>
            <li>Dashboard Link 1</li>
            <li>Dashboard Link 2</li>
          </ul>
        </nav>
      </aside>
      <main>{children}</main>
    </div>
  );
}

This DashboardLayout will wrap any page under the /dashboard route, providing a specific layout for the dashboard section of your application.

 

Step 5: Creating a Layout for Authentication

Similarly, you might want a different layout for your authentication pages (login, signup). Create a layout.js file under the app/auth/ directory:

// app/auth/layout.js
export default function AuthLayout({ children }) {
  return (
    <div className="auth-layout">
      <header>
        <h2>Authentication Header</h2>
      </header>
      <main>{children}</main>
      <footer>
        <p>Auth Footer</p>
      </footer>
    </div>
  );
}

This layout will be used specifically for pages under the /auth route.

 

Step 6: Adding Page Components

Now that you've defined your layouts, you can create page components that will use these layouts automatically. For example, create a page.js file for the dashboard and authentication routes:

// app/dashboard/page.js
export default function DashboardPage() {
  return <h2>Welcome to the Dashboard</h2>;
}

// app/auth/page.js
export default function AuthPage() {
  return <h2>Login or Signup</h2>;
}

These pages will automatically use their respective layouts (DashboardLayout and AuthLayout) defined in their folders.

 

Testing the Layouts

Now, when you navigate to the /dashboard and /auth routes in your application, you will see that each section has its unique layout applied, while the global layout remains present for other pages, ensuring a consistent user experience. Below are screenshots of the output for both routes.

Fig: Dashboard Route

Fig: Dashboard Route

 

Fig: Auth Route

 

Conclusion

Using multiple root layouts in Next.js with the App Router is a powerful way to structure your application. It allows you to create distinct sections of your app, each with its own design and structure, while still maintaining a global layout. This approach makes your codebase more modular, easier to manage, and more scalable as your app grows.

 

Trendingblogs
The Software Development Life Cycle (SDLC): A Complete Guide for Software Success image

The Software Development Life Cycle (SDLC): A Complete Guide for Software Success

Zustand: A Lightweight State Management Library for React image

Zustand: A Lightweight State Management Library for React

From Bugs to Beauty: Integrating UI/UX Testing into Software Quality Assurance image

From Bugs to Beauty: Integrating UI/UX Testing into Software Quality Assurance

Why is a Gaming PC the Ultimate Tool for Both Gaming and Professional Development? image

Why is a Gaming PC the Ultimate Tool for Both Gaming and Professional Development?

Why React Native is the Best Choice for Cross-Platform Development image

Why React Native is the Best Choice for Cross-Platform Development

Let's Deep Dive Into Threads with Rust image

Let's Deep Dive Into Threads with Rust

Get the best of our content straight to your inbox!

Don’t worry, we don’t spam!

Frequently Asked Questions