...

/

Introduction to React Router

Introduction to React Router

Learn how React Router simplifies navigation and enhances scalability in single-page applications.

React applications often require navigation between different pages or views, such as Home, About, Projects, or Contact pages. Traditionally, this is done by manually managing state or creating multiple HTML files. However, as applications grow, this approach becomes cumbersome and lacks essential features like seamless navigation and URL-based routing. Let’s explore a scenario to build a small web application using conventional web development techniques and understand the challenges it presents. We’ll then solve the same scenario using React Router to highlight its benefits.

Case study: A portfolio application

We need to build a portfolio website with the following pages:

  • Home: A welcome page.

  • About: Information about the individual.

  • Projects: A showcase of completed projects.

  • Contact: A page to get in touch.

Implementation without React Router

First, we’ll implement it without React Router to understand the limitations of managing navigation manually.

Limitations of manual navigation approach

  • No URLs for pages: The application does not use unique URLs for each page, making it impossible to bookmark or share links.

  • Poor user experience: Refreshing the browser resets the app to the default state (Home page).

  • Manual management: Adding new pages requires updating the renderPage function and navigation bar manually.

  • SEO issues: Search engines cannot crawl individual pages due to the lack of URLs.

To address these limitations, we can use React Router, a powerful library designed to handle routing in React applications.

React Router

React Router is the de-facto standard for handling routing in React applications. It allows developers to define routes, navigate between views, and manage URL parameters efficiently. With its robust features and integration capabilities, React Router is essential for building modern single-page applications (SPAs).

Single-page applications (SPAs)

A single-page application (SPA) is a web application that dynamically loads and updates content on a single HTML page. Instead of fetching a new HTML file for each route, SPAs render content dynamically based on the URL. This improves performance and creates a smoother user experience.

Rebuilding the portfolio application using React Router

Here is the same portfolio application implemented using React Router:

Note: Don’t worry about the new coding constructs like RouterRoutesRoute, and Link. We will explore these in detail in the upcoming lessons.

Benefits of using React Router

  • Seamless navigation: No full-page reloads; transitions between views are smooth and fast.

  • URL-based routing: Each route has a unique URL, making it easier to bookmark and share links.

  • Dynamic and scalable: Adding new routes is as simple as defining a new Route component.

  • Improved SEO: SPAs with React Router can be optimized for SEO using server-side rendering or pre-rendering tools.

By transitioning from a manual approach to using React Router, we resolved the limitations of traditional navigation.

Comparison matrix: Manual navigation vs. React Router

Below is a detailed comparison of manual navigation and React Router to highlight their differences and help us understand why React Router is preferred for modern web applications.

Aspect

Manual navigation (Without React Router)

React Router

Navigation

Handled manually using useState and conditional rendering.

Managed automatically using the Routes and Route components.

URLs for pages

No URL updates; all navigation is internal, breaking browser behavior like bookmarks.

Each page has a unique URL, enabling bookmarking and sharing.

Browser refresh

Refresh resets the app to the default state, losing the current page.

Refresh preserves the current page using URL-based routing.

Scalability

Difficult to manage as the app grows; adding new pages requires modifying state and navigation.

Easily scalable; new pages require defining a new Route and component.

SEO optimization

Poor; search engines cannot index individual pages.

Better; supports unique URLs for pages, aiding indexing (with additional tools like SSR for optimization).

Code reusability

Repeated code (e.g., navigation bar) across multiple components.

Common components like navigation are defined once and reused across the app.

Performance

Slower; page state is re-evaluated for every render, especially with many conditions.

Faster; React Router handles rendering efficiently without re-evaluating state manually.

Dynamic routing

Not feasible without significant custom logic.

Supported out of the box with route parameters.

User experience

Less smooth; full-page reloads or manual updates create delays.

Seamless; no full-page reloads, resulting in better performance and experience.

Key takeaways

  • Manual navigation is a quick and straightforward approach for small applications but is not scalable or efficient for larger projects.

  • React Router introduces robust navigation and routing features essential for building modern, scalable, and user-friendly SPAs.