A Comprehensive Guide to React Routers: Routes in ReactJS

Siddharth Rastogi
3 min readAug 8, 2023

--

Hey everyone, we have already discussed how to start with React in the previous article. In this article, we are going to learn one essential feature of your application is navigation between different components i.e. Routes in ReactJS.

This is where React Routers come into play. React Routers are a standard library for routing in React, enabling navigation among views and keeping the UI in sync with the URL.

In this detailed guide, we will explore the ins and outs of React Routers, how to install them, and how to implement them in your React application.

What are React Routers?

React Routers provide a way to handle navigation in a React application. They allow you to define routes and render different components based on the current URL.

React Routers keep the UI in sync with the URL, enabling users to navigate through different views without having to reload the entire page. This enhances the user experience and makes the application feel more like a traditional website.

Installing React Routers

Before we go into using React Routers, we need to install the necessary dependencies. In your terminal, navigate to your project directory and run the below command:

npm install react-router-dom

This command installs the react-router-dom package, which is the most commonly used package for React Routers.

Importing React Router Components

To start using React Routers in your application, you need to import the necessary components from the react-router-dom package. The main components we will be using are BrowserRouter, Routes, Route, and Link. Add the following import statement to your code:

import { BrowserRouter, Routes, Route, Link } from “react-router-dom”;

These components will be used to define routes, render components based on the current URL, and create links for navigation.

Understanding the React Router Components

Let’s take a closer look at the main components involved:

BrowserRouter

The BrowserRouter component is a router implementation that uses the HTML5 history API to keep your UI in sync with the URL. It is the parent component that wraps all other components involved in routing.

Routes

The Routes component is a new addition in React Router v6. It is an upgrade to the previous Switch component. The main advantage of Routes over Switch is that routes are chosen based on the best match instead of being traversed in order. This makes it more flexible and efficient when handling complex route configurations.

Route

The Route component is responsible for conditionally rendering a component based on the current URL. It takes a path prop, which specifies the URL pattern to match, and an element prop, which specifies the component to render when the URL matches the path.

Link

The Link component is used to create links to different routes within your application. It works similarly to an HTML anchor tag, allowing users to click on the link and navigate to a specific route.

Implementing React Routers in Your Application

Now that we have a good understanding of React Routers, let’s move to the implementation.

We will create a simple application with three components: a home component, an about component, and a contact component. So, users will be able to navigate between these components using React Routers.

Creating the Components

In your project directory, create a folder named “components” inside the “src” folder. Inside the components folder, create three files: Home.js, About.js, and Contact.js. Here’s a basic structure for each component:

…..

…..

To read the full article with examples, you can checkout here……..

--

--

Siddharth Rastogi

I am a full stack developer, I have an expertise in Web Development. I write tech stuff and share my knowledge with others through blogs & articles.