CreateBrowserRouter를 사용하여 라우터 설정 🚀반응 (v6 + 패턴)

작성자

카테고리:

← 피드로
DEV Community · codeek · 2026-07-08 개발(SW)

codeek

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.

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다