import { createBrowserRouter } from "react-router-dom"; const router = createBrowserRouter([ { path: "/", element: ( <div> <h1>Hello World</h1> <Link to="about">About Us</Link> </div> ), }, { path: "about", element: <div>About</div>, }, ]);
Cách định nghĩa route trong react-router-dom v6

App.jsx. This approach often became challenging when dealing with complex website structures that involved nested routes with various attributes and functionalities.// v4 and v5 before 5.1 function User({ id }) { // ... } function App() { return ( <Switch> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/users/:id" render={({ match }) => ( <User id={match.params.id} /> )} /> </Switch> ); } // v5.1 preferred style function User() { let { id } = useParams(); // ... } function App() { return ( <Switch> <Route exact path="/"> <Home /> </Route> <Route path="/about"> <About /> </Route> {/* Can also use a named `children` prop */} <Route path="/users/:id" children={<User />} /> </Switch> ); }
An example of defining routing with older versions of React Router DOM can be found in the source: https://reactrouter.com/en/main/upgrading/v5.

// Configure nested routes with JSX createBrowserRouter( createRoutesFromElements( <Route path="/" element={<Root />}> <Route path="contact" element={<Contact />} /> <Route path="dashboard" element={<Dashboard />} loader={({ request }) => fetch("/api/dashboard.json", { signal: request.signal, }) } /> <Route element={<AuthLayout />}> <Route path="login" element={<Login />} loader={redirectIfUser} /> <Route path="logout" action={logoutUser} /> </Route> </Route> ) ); // Or use plain objects createBrowserRouter([ { path: "/", element: <Root />, children: [ { path: "contact", element: <Contact />, }, { path: "dashboard", element: <Dashboard />, loader: ({ request }) => fetch("/api/dashboard.json", { signal: request.signal, }), }, { element: <AuthLayout />, children: [ { path: "login", element: <Login />, loader: redirectIfUser, }, { path: "logout", action: logoutUser, }, ], }, ], }, ]);
Source: https://reactrouter.com/en/main/start/overview#nested-routes

In version 13, Next.js introduced a new App Router built on React Server Components, which supports shared layouts, nested routing, loading states, error handling, and more.