DEV Community

codeek
codeek

Posted on

πŸš€React Router Setup Using createBrowserRouter (v6+ Pattern)

If you're still using BrowserRouter with Routes, it's time to move to the modern React Router pattern using createBrowserRouter.

This approach makes your application more scalable and prepares your project for advanced features like nested layouts, loaders, actions, and protected routes.

πŸ“¦ Step 1: Install React Router

npm install react-router-dom

πŸ“ Recommended Project Structure
src/
β”‚
β”œβ”€β”€ layouts/
β”‚ └── MainLayout.jsx
β”‚
β”œβ”€β”€ pages/
β”‚ β”œβ”€β”€ Home.jsx
β”‚ β”œβ”€β”€ Products.jsx
β”‚ β”œβ”€β”€ ProductDetails.jsx
β”‚ β”œβ”€β”€ Cart.jsx
β”‚ └── NotFound.jsx
β”‚
β”œβ”€β”€ routes/
β”‚ └── index.jsx
β”‚
β”œβ”€β”€ App.jsx
└── main.jsx

Keeping your routes in a dedicated folder makes the application easier to maintain as it grows.

⚑ Step 2: Create Your Router

Create a file:

src/routes/index.jsx

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

import MainLayout from "../layouts/MainLayout";

import Home from "../pages/Home";
import Products from "../pages/Products";
import ProductDetails from "../pages/ProductDetails";
import Cart from "../pages/Cart";
import NotFound from "../pages/NotFound";

export const router = createBrowserRouter([
  {
    path: "/",
    element: <MainLayout />,
    children: [
      {
        index: true,
        element: <Home />,
      },
      {
        path: "products",
        element: <Products />,
      },
      {
        path: "products/:id",
        element: <ProductDetails />,
      },
      {
        path: "cart",
        element: <Cart />,
      },
    ],
  },
  {
    path: "*",
    element: <NotFound />,
  },
]);
Enter fullscreen mode Exit fullscreen mode

The router is created once and supplied to your application through RouterProvider, which is the recommended pattern for modern React Router applications.

πŸ— Step 3: Configure main.jsx

import React from "react";
import ReactDOM from "react-dom/client";

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

import { router } from "./routes";

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

🏠 Step 4: Create a Layout


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

const MainLayout = () => {
  return (
    <>
      <header>Navbar</header>

      <main>
        <Outlet />
      </main>

      <footer>Footer</footer>
    </>
  );
};

export default MainLayout;
Enter fullscreen mode Exit fullscreen mode

is where nested child routes are rendered.

πŸ“ Route Overview

URL Component
/ Home
/products Products
/products/:id Product Details
/cart Cart
* 404 Page

πŸ’‘ Why Use createBrowserRouter?

βœ… Cleaner route configuration

βœ… Supports nested layouts effortlessly

βœ… Built for Data APIs (Loaders & Actions)

βœ… Easier authentication and protected routes

βœ… Better scalability for large React applications

React Router recommends creating the router outside the React tree and passing it to RouterProvider for modern applications.

πŸ”₯ What's Next?

Once your router is configured, you can easily add:

πŸ” Protected Routes
πŸ‘€ Authentication
πŸ“¦ Nested Layouts
⚑ Loaders & Actions
❌ Error Pages
πŸ”„ Lazy Loading
🌍 Dynamic Routes
πŸŽ₯ Learn React Router the Practical Way

If you'd like to see everything built step by stepβ€”from installation to nested routes, protected routes, authentication, and real-world project structureβ€”check out this tutorial:

πŸ“Ί React Router Complete Setup

https://www.youtube.com/watch?v=euk5pWCOIyg&list=PL_02r0p8Ku_6tR8L-n-yj7MW8rrMtswwk&index=4

You'll learn:

βœ… Modern React Router setup
βœ… createBrowserRouter
βœ… Nested Routing
βœ… Layouts
βœ… Navigation
βœ… Best Folder Structure
πŸ›’ Build a Production-Ready Ecommerce Application

Want to apply React Router in a real project?

Watch the complete Ecommerce series where we build everything from scratch using modern React technologies:

Tech Stack

βš›οΈ React
πŸ›£ React Router
🎨 Chakra UI
🐻 Zustand
πŸ’³ Stripe Payment Integration
πŸ“‘ API Integration
πŸ›’ Shopping Cart
πŸš€ Production Best Practices

πŸ“Ί Full Ecommerce Playlist

https://www.youtube.com/watch?v=Jluy-W9eP-4&list=PL_02r0p8Ku_6tR8L-n-yj7MW8rrMtswwk&index=1

If you're aiming to build production-ready React applications instead of simple demos, this series is designed to help you understand how these technologies work together in a real-world ecommerce project.

Top comments (0)