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:

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:

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.

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:

Home.js:

About.js:

Contact.js:

Setting up the Router

Now, let’s set up the router in our main App component. Import the necessary components from react-router-dom and the three components we created earlier. Here’s an example of how the “App.js” file should look:

In this code, we import the necessary components and set up the basic structure of our application.

Testing the Navigation

Now that we have set up the router, let’s test the navigation in our application. Start the development server by running the following command:

Open your browser and navigate to http://localhost:3000. You should see the home component rendered by default. Click on the links in the header to navigate to the About and Contact components. The URL will change accordingly, and the corresponding component will be rendered.

Congratulations! You have successfully implemented navigation in your React application using React Routers.


Now that you have a solid understanding of React Routers in the application, you can explore more advanced features and configurations to further customize your application’s navigation.

I hope you enjoyed the article and if you found this useful, then please share it with your friends and colleagues.

If you have any queries please feel free to post them in the comments section or anything that you wanted to ask through mail contact.

Happy coding! 😉.

Also read,