Defining and Using Routes
Defining routes in React means telling React Router which component to show for each URL path, such as the home page at /, a product list at /products and a single product at /products/:id. The router watches the address bar and swaps components without reloading the page, which is what makes a single page application feel like a multi-page website. This lesson uses React Router version 6 and later.
Wrapping the app in a router
After installing react-router-dom in the previous lesson, the first step is wrapping your app in BrowserRouter, usually in main.jsx. It connects React Router to the browser's history so that URL changes trigger re-renders.
// main.jsx
import { BrowserRouter } from 'react-router-dom';
createRoot(document.getElementById('root')).render(
<BrowserRouter>
<App />
</BrowserRouter>
);
Declaring routes with Routes and Route
Inside the app, a <Routes> element holds a list of <Route> elements. Each one has a path and an element, the JSX to show when the path matches. React Router picks the best match automatically, regardless of the order you list them in.
import { Routes, Route, Outlet, useParams } from 'react-router-dom';
function Layout() {
return (
<>
<header>My Shop</header>
<main>
<Outlet /> {/* child route renders here */}
</main>
</>
);
}
function ProductDetail() {
const { productId } = useParams();
return <h2>Product {productId}</h2>;
}
export default function App() {
return (
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="products" element={<ProductList />} />
<Route path="products/:productId" element={<ProductDetail />} />
<Route path="about" element={<About />} />
<Route path="*" element={<NotFound />} />
</Route>
</Routes>
);
}
Nested routes and the shared layout
Routes can be nested. In the example, every page sits inside the Layout route, so the header appears on all of them. <Outlet /> marks the spot where the matching child route renders. Child paths without a leading slash are relative to their parent, so products under / becomes /products.
The index route is the default child, shown when the URL is exactly the parent's path. Here it shows Home at /. The path="*" route matches anything else and is the standard way to show a "page not found" screen.
Dynamic segments
A segment starting with a colon, like :productId, matches any value in that position. Inside the component, useParams() returns an object with those values, so visiting /products/17 gives { productId: '17' }. As with all URL values, it is a string, so convert it before comparing with numeric ids. Components typically use this value to fetch the right item from an API.
Old syntax that no longer works
Many tutorials still show React Router version 5 code. If you copy it into a version 6 project, it fails. The main differences are:
<Switch>was replaced by<Routes>.component={Home}andrenderprops becameelement={<Home />}.- The
exactprop is gone; matching is exact by default, and nested routes replace the need for it. useHistorywas replaced byuseNavigate.
A second mistake is navigating with ordinary <a href> tags. They work, but they make the browser reload the whole page, losing all React state. The next lesson introduces Link, which changes the URL without a reload.
Takeaway
Wrap the app in BrowserRouter, map paths to elements with Routes and Route, share layout through nested routes and Outlet, use index and * for the default and fallback pages, and read dynamic segments with useParams.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.