🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

Route

AI & DATA SCIENCE // route

Route defines a mapping between a URL path pattern and the component that should render when the current URL matches that pattern.

Syntax

<Route path="/products/:id" element={<ProductPage />} />

Deep Dive Course

A Route is declared inside a Routes container (React Router v6+) and specifies a path pattern, which can include static segments, dynamic segments prefixed with a colon like :id, and wildcards, along with an element prop specifying the component to render when the current URL matches that pattern. Routes checks all its child Route elements and renders the single best match for the current URL — in v6, matching considers specificity, so a more specific static route generally takes priority over a more general dynamic one, but only one Route ultimately renders per Routes.

1Understanding Route

A Route is declared inside a Routes container (React Router v6+) and specifies a path pattern, which can include static segments, dynamic segments prefixed with a colon like :id, and wildcards, along with an element prop specifying the component to render when the current URL matches that pattern. Routes checks all its child Route elements and renders the single best match for the current URL — in v6, matching considers specificity, so a more specific static route generally takes priority over a more general dynamic one, but only one Route ultimately renders per Routes.

💡

In React Router v6, use the element prop, <Route path="..." element={<Component />} />, not the older component prop from v5 — the two versions have meaningfully different Route APIs, so mixing documentation from different versions is a common source of confusion.

editor.html
import { Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/products/:id" element={<ProductPage />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}
localhost:3000

2Practical Example

Here is a real-world application of Route showing how it is used in production React code.

editor.html
import { Routes, Route, Outlet } from 'react-router-dom';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Layout />}>
        <Route path="about" element={<About />} />
      </Route>
    </Routes>
  );
}

function Layout() {
  return (
    <div>
      <Nav />
      <Outlet />
    </div>
  );
}
localhost:3000

3Best Practices

Follow these guidelines when working with Route:

1. Use the element prop with JSX, <Route element={<Component />} />, for React Router v6, rather than the older v5-style component prop

2. Order or structure routes so more specific paths are matched correctly against more general or wildcard ones, understanding v6's own path-ranking matching algorithm

3. Use nested Route elements for layouts shared across several child routes, rendering the shared layout once with an Outlet for the matched child route's content

⚠️

Tip: In React Router v6, use the element prop, <Route path="..." element={<Component />} />, not the older component prop from v5 — the two versions have meaningfully different Route APIs, so mixing documentation from different versions is a common source of confusion.

editor.html
import { Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/products/:id" element={<ProductPage />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}
localhost:3000

Examples

Example 01Basic Usage
import { Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/products/:id" element={<ProductPage />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}
Example 02Advanced Example
import { Routes, Route, Outlet } from 'react-router-dom';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Layout />}>
        <Route path="about" element={<About />} />
      </Route>
    </Routes>
  );
}

function Layout() {
  return (
    <div>
      <Nav />
      <Outlet />
    </div>
  );
}

Best Practices

  • Use the element prop with JSX, } />, for React Router v6, rather than the older v5-style component prop
  • Order or structure routes so more specific paths are matched correctly against more general or wildcard ones, understanding v6's own path-ranking matching algorithm
  • Use nested Route elements for layouts shared across several child routes, rendering the shared layout once with an Outlet for the matched child route's content

Interview Question

Why does React Router v6 need an Outlet component when using nested routes, rather than the child route's element simply appearing automatically inside the parent route's rendered output?

Hint: Think about how React actually knows where, specifically, within a parent component's own JSX, a nested child's content should be inserted.

A parent route's element is just a regular React component that returns its own specific JSX, like a layout with a navigation bar, a sidebar, and various other markup arranged in a particular structure, and React has no built-in, automatic way to know which specific spot within that arbitrary JSX structure a nested child route's content is supposed to be inserted into. Outlet solves this by acting as an explicit placeholder that the parent route's own component includes directly in its JSX at exactly the spot where nested child content should appear, and React Router then renders whichever child route currently matches directly into that specific Outlet location. Without this explicit placeholder, there would be no way to specify where, within a potentially complex parent layout, the matched child route's content should actually be rendered.

Exercises

MediumPractice using Route in a real scenario.
View Solution
import { Routes, Route } from 'react-router-dom';

function App() {
  return (
    <Routes>
      <Route path="/" element={<Home />} />
      <Route path="/products/:id" element={<ProductPage />} />
      <Route path="*" element={<NotFound />} />
    </Routes>
  );
}

Frequently Asked Questions

Why does React Router v6 need an Outlet component when using nested routes, rather than the child route's element simply appearing automatically inside the parent route's rendered output?

A parent route's element is just a regular React component that returns its own specific JSX, like a layout with a navigation bar, a sidebar, and various other markup arranged in a particular structure, and React has no built-in, automatic way to know which specific spot within that arbitrary JSX structure a nested child route's content is supposed to be inserted into. Outlet solves this by acting as an explicit placeholder that the parent route's own component includes directly in its JSX at exactly the spot where nested child content should appear, and React Router then renders whichever child route currently matches directly into that specific Outlet location. Without this explicit placeholder, there would be no way to specify where, within a potentially complex parent layout, the matched child route's content should actually be rendered.

Related Functions

linkroutingswitch