React Routing Introduction
In this lesson, we will learn what routing is, why React applications need routing, and how URLs can display different pages without reloading the entire application.
1. What Is Routing?
Routing is the process of displaying different content based on the URL requested by the user. In a React application, routing helps us navigate between pages such as Home, About, Courses, and Contact.
For example, when a user visits /about, the application can display the About page. When the user visits /courses, it can display the Courses page.
Remember: Routing connects a URL to the component that should be displayed for that URL.
2. Why Do We Need Routing?
A real-world React application usually contains multiple sections. Routing allows users to move between these sections in an organized way.
- To display different pages using different URLs.
- To navigate between application sections.
- To support URLs that users can bookmark or share.
- To build structured applications with multiple views.
- To create navigation for dashboards, courses, products, and profiles.
3. Example of Routing in a Training Institute
Consider a website for CIIT Training Institute. It may contain a Home page, Courses page, About page, and Contact page. Each URL can display the appropriate page component.
Displays the Home component
Displays the Courses component
Displays the About component
Displays the Contact component
4. Traditional Website Navigation vs React Routing
Traditional Website Navigation
In a traditional website, clicking a link commonly sends a new request to the server and loads another HTML document.
<a href="/about">About</a>
React Application Navigation
In a React single-page application, client-side routing can change the displayed component while keeping the main application document loaded.
<Link to="/about">About</Link>
Note: The Link component belongs to React Router. We will learn how to install React Router and use Link in upcoming lessons.
5. What Is a Single-Page Application?
A Single-Page Application (SPA) loads its main HTML document and updates the displayed interface as users interact with it. Client-side routing can display different views without requiring a complete page reload for every navigation.
For example, a user can navigate from the Home page to the Courses page while React Router updates the view.
6. What Is React Router?
React Router is a routing library used to manage navigation in React applications. It provides components and hooks that help connect URLs with React components.
- BrowserRouter: Provides browser-based routing.
- Routes: Groups route definitions.
- Route: Maps a URL path to an element.
- Link: Navigates to another route.
- NavLink: Supports navigation links with active-route styling.
- useNavigate: Enables programmatic navigation.
- useParams: Reads dynamic URL parameters.
We will study these components and hooks individually in the upcoming lessons of Module 13.
7. Example: Website Pages and URLs
The following table shows a simple example of how different URLs can correspond to different pages in a React application.
| URL Path | Page Component |
|---|---|
| / | Home |
| /courses | Courses |
| /about | About |
| /contact | Contact |
Practice Tasks
- Define routing in your own words.
- Explain why routing is useful in React applications.
- Write four example URL paths for a training institute website.
- Differentiate between traditional navigation and client-side routing.
- Explain what a Single-Page Application is.
- List any four components or hooks provided by React Router.
- Give two real-world examples where routing may be useful.
Summary
React routing allows an application to display different components based on the URL. It helps organize pages, supports navigation, and makes it easier to build applications with multiple views. React Router is a popular library that provides tools such as BrowserRouter, Routes, Route, Link, NavLink, useNavigate, and useParams. In the next lesson, we will learn more about React Router and how it works.