DEV Community

codeek
codeek

Posted on

πŸš€ 10x your project workflow with Reusable Layouts in React Router V6 with createBrowserRouter and Nested Routes

If you've ever copied the same Navbar, Sidebar, or Footer across multiple pages, you're doing extra work.

React Router's createBrowserRouter, children, and Outlet let you create reusable layouts that automatically wrap multiple pages. Instead of repeating components everywhere, you define them once and let nested routes handle the rest.

In this guide, we'll build a clean routing structure that scales as your application grows.


Why Use Nested Routes?

Imagine your application has the following pages:

  • Home
  • Login
  • Register
  • Dashboard
  • Profile
  • Settings

The authentication pages shouldn't display the dashboard sidebar.

The dashboard pages, however, should all share:

  • Navigation
  • Sidebar
  • Footer

Instead of repeating these components inside every page, we'll create a reusable layout.

React Router supports this through nested routing using the children property and renders child pages through the Outlet component.


Project Structure

src
β”‚
β”œβ”€β”€ layouts
β”‚   β”œβ”€β”€ MainLayout.jsx
β”‚   └── DashboardLayout.jsx
β”‚
β”œβ”€β”€ pages
β”‚   β”œβ”€β”€ Home.jsx
β”‚   β”œβ”€β”€ Login.jsx
β”‚   β”œβ”€β”€ Register.jsx
β”‚   β”œβ”€β”€ Dashboard.jsx
β”‚   β”œβ”€β”€ Profile.jsx
β”‚   └── Settings.jsx
β”‚
β”œβ”€β”€ routes
β”‚   └── router.jsx
β”‚
└── main.jsx
Enter fullscreen mode Exit fullscreen mode

Step 1 β€” Install React Router

npm install react-router-dom
Enter fullscreen mode Exit fullscreen mode

Step 2 β€” Create the Router

React Router recommends creating your router once using createBrowserRouter and passing it to RouterProvider.

import { createBrowserRouter } from "react-router-dom";

export const router = createBrowserRouter([
  {
    path: "/",
    element: <MainLayout />,
    children: [
      {
        index: true,
        element: <Home />,
      },
      {
        path: "login",
        element: <Login />,
      },
      {
        path: "register",
        element: <Register />,
      },
    ],
  },
]);
Enter fullscreen mode Exit fullscreen mode

Step 3 β€” Create a Layout Component

Instead of rendering pages directly, create a reusable layout.

import { Outlet } from "react-router-dom";

function MainLayout() {
  return (
    <>
      <Navbar />

      <main>
        <Outlet />
      </main>

      <Footer />
    </>
  );
}

export default MainLayout;
Enter fullscreen mode Exit fullscreen mode

The magic happens with Outlet.

Whenever one of the child routes matches, React Router renders it exactly where the Outlet is placed.


Step 4 β€” Create a Dashboard Layout

Now let's create a completely different layout.

{
  path: "/dashboard",
  element: <DashboardLayout />,
  children: [
    {
      index: true,
      element: <Dashboard />,
    },
    {
      path: "profile",
      element: <Profile />,
    },
    {
      path: "settings",
      element: <Settings />,
    },
  ],
}
Enter fullscreen mode Exit fullscreen mode

Dashboard layout:

import { Outlet } from "react-router-dom";

function DashboardLayout() {
  return (
    <div className="dashboard">
      <Sidebar />

      <section className="content">
        <Outlet />
      </section>
    </div>
  );
}

export default DashboardLayout;
Enter fullscreen mode Exit fullscreen mode

Every dashboard page now automatically shares the sidebar.

No duplicated code.


Step 5 β€” Wrap Everything with RouterProvider

import ReactDOM from "react-dom/client";
import { RouterProvider } from "react-router-dom";
import { router } from "./routes/router";

ReactDOM.createRoot(document.getElementById("root")).render(
  <RouterProvider router={router} />
);
Enter fullscreen mode Exit fullscreen mode

Final Route Structure

/

β”œβ”€β”€ Home
β”œβ”€β”€ Login
β”œβ”€β”€ Register

/dashboard
β”œβ”€β”€ Dashboard
β”œβ”€β”€ Profile
└── Settings
Enter fullscreen mode Exit fullscreen mode

Why This Pattern is Better

  • βœ… No duplicated Navbar or Sidebar
  • βœ… Cleaner project structure
  • βœ… Easier to maintain
  • βœ… Better scalability
  • βœ… Perfect for authentication layouts
  • βœ… Great for admin dashboards

Key Takeaways

  • Use createBrowserRouter to define your application's routes.
  • Organize related pages with the children property.
  • Wrap shared UI inside layout components.
  • Render nested pages using Outlet.
  • Keep routing scalable and easy to maintain as your application grows.

πŸŽ₯ Prefer Learning by Watching?

This article covers the concepts step by step, but if you'd like to see the complete implementation in actionβ€”including project setup, nested layouts, authentication routing, and best practicesβ€”check out Episode 6 of my React Authentication & Authorization series.

πŸ“Ί Watch here:

https://www.youtube.com/watch?v=n0evocfZls0&list=PL_02r0p8Ku_5-h4teExCf6egkktSQblC4&index=6

The full playlist walks through building a modern authentication system with React Router, protected routes, JWT authentication, authorization, and much more from scratch.

Happy coding! πŸš€

Top comments (0)