Routes & Route
Routes and Route cover route tables, matching, layout routes with Outlet, index routes, not-found wildcards, and maintainable route organization.
Course: React — Routing & API Integration Lesson: 03
Learning objectives
Learn how Routes and Route work together, how route matching is expressed, and how to build a maintainable route table.
The responsibilities
Routes examines its route definitions and renders the best matching branch for the current location. A Route declares a path pattern and the element to render when that pattern matches.
import { Routes, Route } from "react-router-dom";function AppRoutes() {return (<Routes><Route path="/" element={<HomePage />} /><Route path="/about" element={<AboutPage />} /><Route path="/contact" element={<ContactPage />} /></Routes>);}
A route element is a React element, commonly written as <PageComponent />. The route configuration describes navigation structure; it is not itself a page component.
A route table for a SaaS product
function AppRoutes() {return (<Routes><Route path="/" element={<LandingPage />} /><Route path="/login" element={<LoginPage />} /><Route path="/register" element={<RegisterPage />} /><Route path="/dashboard" element={<DashboardPage />} /><Route path="/customers" element={<CustomerListPage />} /><Route path="/customers/:customerId" element={<CustomerDetailsPage />} /><Route path="*" element={<NotFoundPage />} /></Routes>);}
The wildcard route is a useful fallback for unknown URLs. Keep it after the meaningful route declarations for readability, even though React Router performs route ranking rather than relying on simple first-match ordering.
Layout routes
A route can render a layout that contains an Outlet. This allows a shared header, sidebar, or workspace shell to remain in place while child routes change.
import { Outlet } from "react-router-dom";function WorkspaceLayout() {return (<div className="workspace"><Sidebar /><main><TopBar /><Outlet /></main></div>);}
<Routes><Route path="/workspace" element={<WorkspaceLayout />}><Route index element={<DashboardPage />} /><Route path="customers" element={<CustomerListPage />} /><Route path="orders" element={<OrderListPage />} /></Route></Routes>
The index route renders at /workspace. The child paths are relative to the parent, producing /workspace/customers and /workspace/orders.
Not-found handling
Use a wildcard route for a friendly 404 page. It should explain that the resource or page was not found and provide a safe route back to the application.
Route organization
For a small application, a single route file may be sufficient. For a larger product, split route groups by feature or access area while keeping a clear root configuration. Avoid scattering duplicate route declarations throughout page components.
Common mistakes
- Forgetting to render
<Outlet />in a parent layout. - Writing child paths with unnecessary leading slashes when using relative nested routes.
- Rendering a page directly instead of declaring it in the route tree.
- Omitting a not-found route.
- Assuming route matching protects backend endpoints.
Best practices
- Keep route definitions declarative.
- Use layouts to share persistent UI.
- Use index routes for the default child screen.
- Keep route paths consistent and resource-oriented.
- Add tests for expected and unknown URLs.
Interview questions & answers
Interview preparation
5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.
Beginner
1What is the difference between Routes and Route?
BeginnerModel answer
Routesevaluates child definitions and renders the best matching branch. ARoutedeclares a path pattern and the element to render when that pattern matches.2How does React Router choose a matching route?
BeginnerModel answer
It ranks route definitions by specificity and pattern ranking rather than simple first-match ordering, then renders the winning branch.
Intermediate
3What is an index route?
IntermediateModel answer
An index route is the default child content rendered at the parent path, such as the dashboard at/workspacewhen no extra child segment is present.4How would you organize route definitions in a large application?
IntermediateModel answer
Keep a clear root configuration and split route groups by feature or access area, avoiding duplicate declarations scattered through unrelated page components.
Advanced
5What does Outlet render?
AdvancedModel answer
Outletis the placeholder in a parent layout where the matched child route element is rendered.
Practice
Create a workspace layout with index, customers, orders, and a wildcard not-found route. Confirm the sidebar remains visible as child routes change.