Using Route and Link Components

Tutorial 2 of 5

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!