Routes and Route Components
In this lesson, we will learn how Routes and Route work together to display the correct React component for a particular URL.
1. What Is a Route?
A Route is a React Router component that associates a URL path with an element to render when that route matches the current location.
For example, the path /about can display an About component, while /courses can display a Courses component.
Remember: Route defines the path and the component that should be displayed for that path.
2. What Is Routes?
Routes is a component that groups Route definitions and renders the best matching route for the current URL.
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
In this example, Routes contains two route definitions: one for the Home page and another for the About page.
3. How Routes and Route Work Together
React Router compares the current URL with the configured route paths. When a route matches, its element is rendered.
4. Understanding the path and element Props
path
The path prop specifies the URL pattern that the route should match.
element
The element prop specifies the React element that should be rendered when the route matches.
<Route
path="/courses"
element={<Courses />}
/>
Here, the Courses component is rendered when the /courses route matches.
5. Create Page Components
First, create the components that represent the pages of your application.
function Home() {
return <h2>Welcome to CIIT Training Institute</h2>;
}
function About() {
return <h2>About CIIT Training Institute</h2>;
}
function Courses() {
return <h2>Our Training Courses</h2>;
}
function Contact() {
return <h2>Contact Us</h2>;
}
Each component represents a separate view in the React application.
6. Configure Multiple Routes
Import Routes and Route from React Router and define the required paths inside the application's existing BrowserRouter.
import { Routes, Route } from 'react-router-dom';
function Home() {
return <h2>Home Page</h2>;
}
function About() {
return <h2>About Page</h2>;
}
function Courses() {
return <h2>Courses Page</h2>;
}
function Contact() {
return <h2>Contact Page</h2>;
}
function App() {
return (
<div>
<h1>CIIT Training Institute</h1>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/courses" element={<Courses />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</div>
);
}
export default App;
Important: This App example must be rendered inside the BrowserRouter configured in the previous lesson. Do not add a second BrowserRouter around the same application.
7. Expected Output
When the browser opens the root URL:
CIIT Training Institute
Home Page
When the browser opens /about:
CIIT Training Institute
About Page
When the browser opens /courses:
CIIT Training Institute
Courses Page
When the browser opens /contact:
CIIT Training Institute
Contact Page
8. Add a Not Found Route
You can define a fallback route using the wildcard path *. It matches a URL that does not match another configured route.
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<h2>Page Not Found</h2>} />
</Routes>
For example, an unconfigured URL such as /unknown displays Page Not Found.
We will explore not-found routes in detail in a separate lesson later in this module.
Practice Tasks
- Explain the difference between Routes and Route.
- Explain the purpose of the path prop.
- Explain the purpose of the element prop.
- Create Home, About, Courses, and Contact components.
- Configure routes for all four components.
- Test each URL in the browser.
- Add a wildcard route for unknown URLs.
- Write the expected output for each configured route.
Summary
Routes groups route definitions, while each Route associates a URL path with the React element to display when that route matches. The path prop specifies the URL pattern, and the element prop specifies the component to render. Multiple routes can represent different application pages, and a wildcard route can handle unmatched paths. In the next lesson, we will learn navigation using Link and NavLink.