React.js / React Routing and Navigation

Using Route and Link Components

In this tutorial, you'll learn how to use the Route and Link components to create a basic navigation structure in your React application.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers implementing client-side routing and navigation with React Router.

1. Introduction

In this tutorial, we'll cover the basics of using the Route and Link components in React, which are part of the React Router library. React Router is used to create a navigation structure in your React application.

By the end of the tutorial, you will learn how to use the Route and Link components to create a simple navigation system for your React application.

Prerequisites

Before starting this tutorial, you should have the following:
- A basic understanding of JavaScript and React.
- Node.js and npm installed on your local development environment.
- A text editor, such as VS Code.
- A web browser for testing the application.

2. Step-by-Step Guide

Route Component

The Route component is one of the most important components in React Router. It allows you to create different routes for your application. Each Route component renders a UI when the current location matches the route’s path.

Link Component

The Link component is used for creating links in your application. It navigates the user to different routes.

3. Code Examples

Let's create a new React application and install the React Router DOM.

First, create a new React application:

npx create-react-app react-router-example

Next, navigate to the new directory:

cd react-router-example

Then, install React Router DOM:

npm install react-router-dom

Example 1: Using Route and Link

In this example, we'll create a simple navigation system with two pages: Home and About.

First, open the App.js file and replace its content with the following code:

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

// Components for each page
function Home() {
  return <h2>Home</h2>;
}

function About() {
  return <h2>About</h2>;
}

// Main App component
function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link> {/* Link to the Home page */}
            </li>
            <li>
              <Link to="/about">About</Link> {/* Link to the About page */}
            </li>
          </ul>
        </nav>

        <Route path="/" exact component={Home} /> {/* Route for the Home page */}
        <Route path="/about" component={About} /> {/* Route for the About page */}
      </div>
    </Router>
  );
}

export default App;

In this code:

  • We've imported the necessary components from react-router-dom.
  • We've defined two simple components: Home and About.
  • We've used the Link component to create links to the Home and About pages.
  • We've used the Route component to specify what component should be rendered for each path. The exact prop in the Home route means it will only match if the path is exactly /.

Run the app with npm start. You should see a simple navigation bar at the top, and you can navigate between the Home and About pages.

4. Summary

In this tutorial, you've learned how to use the Route and Link components in React Router to create a basic navigation system. You've also seen how to use the exact prop to make a Route match only the exact path.

The next steps in your learning journey might include exploring other features of React Router, such as nested routes, redirects, and route parameters.

5. Practice Exercises

  1. Add another page to the application. Create a new Link and Route for this page, and verify that you can navigate to it.
  2. Modify the About component to display some information about yourself. This could be your name, your favorite programming language, or anything else you want to share.
  3. Experiment with the exact prop. What happens if you remove it from the Home route? Can you explain why?

Remember, the best way to learn is by doing! Practice what you've learned in this tutorial, experiment with the code, and don't be afraid to make mistakes. That's how we all learn!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help