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 />,
},
]);
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>
);
π 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;
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)