codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
▲

Next.js

21 / 73 topics
21Layout Component22Custom App Component23Custom Document Component
Tutorials/Next.js/Layout Component
▲Next.js

Layout Component

Updated 2026-04-20
3 min read

Layout Component

In this section, we will explore how to create and utilize a Layout Component in Next.js. A layout component is a reusable React component that wraps other components to provide consistent structure, styles, or behaviors across different pages of your application. This approach enhances code reusability, maintainability, and scalability.

Introduction

Next.js provides several ways to manage layouts, but one of the most common and flexible methods is by using custom layout components. These components can include shared navigation bars, footers, sidebars, or any other UI elements that should be consistent across multiple pages.

Creating a Layout Component

Let's start by creating a basic layout component. We'll create a simple MainLayout component that includes a header and footer.

Step 1: Create the Layout Component

First, create a new file named MainLayout.js in your project's components directory:

// components/MainLayout.js
import React from 'react';

const MainLayout = ({ children }) => {
  return (
    <div className="layout">
      <header>
        <h1>My App</h1>
        <nav>
          <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/about">About</a></li>
            <li><a href="/contact">Contact</a></li>
          </ul>
        </nav>
      </header>

      <main>{children}</main>

      <footer>
        <p>&copy; 2023 My App. All rights reserved.</p>
      </footer>
    </div>
  );
};

export default MainLayout;

Step 2: Apply the Layout to a Page

Next, we need to apply this layout to a page in our Next.js application. Let's modify the index.js file located in the pages directory:

// pages/index.js
import React from 'react';
import MainLayout from '../components/MainLayout';

const HomePage = () => {
  return (
    <div>
      <h2>Welcome to My App</h2>
      <p>This is the home page.</p>
    </div>
  );
};

export default function IndexPage() {
  return (
    <MainLayout>
      <HomePage />
    </MainLayout>
  );
}

In this example, we wrap the HomePage component with the MainLayout component. This ensures that the layout is applied to the home page.

Advanced Layout Usage

Dynamic Layouts

Sometimes, you might want different layouts for different pages or groups of pages. You can achieve this by creating multiple layout components and applying them conditionally based on the page.

Example: Admin Layout

Create an AdminLayout.js file in the components directory:

// components/AdminLayout.js
import React from 'react';

const AdminLayout = ({ children }) => {
  return (
    <div className="admin-layout">
      <header>
        <h1>Admin Panel</h1>
        <nav>
          <ul>
            <li><a href="/admin/dashboard">Dashboard</a></li>
            <li><a href="/admin/users">Users</a></li>
            <li><a href="/admin/settings">Settings</a></li>
          </ul>
        </nav>
      </header>

      <main>{children}</main>

      <footer>
        <p>&copy; 2023 Admin Panel. All rights reserved.</p>
      </footer>
    </div>
  );
};

export default AdminLayout;

Apply this layout to an admin page:

// pages/admin/dashboard.js
import React from 'react';
import AdminLayout from '../../components/AdminLayout';

const DashboardPage = () => {
  return (
    <div>
      <h2>Admin Dashboard</h2>
      <p>This is the admin dashboard.</p>
    </div>
  );
};

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

Nested Layouts

You can also nest layouts to create more complex structures. For example, you might have a MainLayout that wraps an AdminLayout.

// pages/admin/_app.js
import React from 'react';
import MainLayout from '../../components/MainLayout';
import AdminLayout from '../../components/AdminLayout';

const AdminApp = ({ Component, pageProps }) => {
  return (
    <MainLayout>
      <AdminLayout>
        <Component {...pageProps} />
      </AdminLayout>
    </MainLayout>
  );
};

export default AdminApp;

In this example, the AdminApp component wraps both the MainLayout and AdminLayout, ensuring that all admin pages have access to both layouts.

Best Practices

  1. Keep Layouts Simple: Ensure that your layout components are focused on structure and do not include complex business logic or UI elements that should be part of individual page components.
  2. Use CSS Modules for Styling: To avoid style conflicts, use CSS Modules in your layout components to scope styles locally.
  3. Optimize Performance: If you have multiple layouts, consider using dynamic imports to load them only when needed, especially if they are large or complex.
  4. Maintain Consistency: Ensure that all pages using the same layout maintain consistent UI elements and behaviors.

Conclusion

Layout components in Next.js are a powerful tool for creating reusable and maintainable structures across your application. By following best practices and leveraging the flexibility of custom layouts, you can build scalable and efficient web applications with ease.

Feel free to experiment with different layouts and configurations to suit the needs of your project. Happy coding!


PreviousTheming with Next.jsNext Custom App Component

Recommended Gear

Theming with Next.jsCustom App Component