What is React Router?

React Router is considered as the standard library for routing in React which allows the navigation among views of various components in any React Application, enables changing the browser URL, and keeps the UI in sync with the URL.

In order to understand how the React router operates, let us create a simple application to React. The application will contain three components, namely, home component, about component, and contact component. We will be using React Router to navigate between these components.

Installing React Router

React Router can be installed via npm ****in the React application. Follow the below steps to install Router in any React application:

  • Step 1: Create a react application by the following command.
npx create-react-app demo-router
  • Step 2: Use cd to go to the project directory i.e., demo-router.
  • Step 3: To install the React Router use the following command:
npm install react-router-dom –save

Components of React Router

The major Components of the React Router are:

  • BrowserRouter: BrowserRouter is actually a router implementation which uses the HTML5 history API i.e., pushState, replaceState and the popstate event in order to keep the UI in sync with the URL. It is the parent component which is used to store all of the other components.
  • Route: Route is basically the conditionally shown component which renders some UI when its path matches the current URL.
  • Link: The Link component is used to create links to different routes and implement navigation around the application and it works like HTML anchor tag
  • Switch: The Switch component is used to render only the first route that matches the location rather than rendering all the matching routes. Although there is no defying functionality of SWITCH tag in our example application as none of the LINK paths are ever going to coincide. But let’s say we have a route, then all the Route tags are going to be processed which start with ‘/’. This is when we need SWITCH statement to process only one of the statements.

Using React Router

To use the React Router, let us first create a few components in the react application. In our project directory, let us create a folder named component inside the src folder and now add three more files named home.js, about.js and contact.js to the component folder.

After creating these files, let us add some code to our components:

Home Component

import React from 'react';
  
function Home (){
    return <h1>Welcome to the Demo Routing App!</h1>
}
  
export default Home;

About Component

import React from 'react';
  
function About () {
    return <div>
        <h2>It shows a demo!</h2>         
    </div>
}
export default About;

Contact Component

import React from 'react';
  
function Contact (){
 return <div>
            <h2>Phone : 98XXXXXXX22</h2>
        </div>
}  
export default Contact;

Now that we have created our components, let us include React Router components to the application in the following manner:

  • BrowserRouter: Add BrowserRouter aliased as Router to the app.js file in order to wrap all the other components. BrowserRouter acts as a parent component and can have only single child.
class App extends Component {
  render() {
    return (
       <Router>
           <div className="App">
           </div>
       </Router>
   );
  }
}
  • Link: Let us now create links to our components. Link component uses the to prop to describe the location where the links should navigate to.
<div className="App">
    <ul>
      <li>
        <Link to="/">Home</Link>
      </li>
      <li>
        <Link to="/about">About Us</Link>
      </li>
      <li>
        <Link to="/contact">Contact Us</Link>
      </li>
    </ul>
</div>

Now, let us run the application on the local host and click on the links that we created. We will notice the URL changing according the value in to props of the Link component.

  • Route: Route component will now help us to establish the link between component’s UI and the URL. To include routes to the application, let us add the code shown below to the app.js.
<Route exact path='/' component={Home}></Route>
<Route exact path='/about' component={About}></Route>
<Route exact path='/contact' component={Contact}></Route>
  • Switch: In order to render a single component, wrap all the routes inside the Switch Component. Switch groups together several routes, iterates over them and finds the first one that matches the path. Thereby, the corresponding component to the path is rendered.
<Switch>
    <Route exact path='/' component={Home}></Route>
    <Route exact path='/about' component={About}></Route>
    <Route exact path='/contact' component={Contact}></Route>
</Switch>

Summary

  • In this article we learnt the concept of Routing in ReactJS along with its various components like BrowserRouter, Link and others.
  • We also learnt how to install React Router in any React application.
  • We also understood the actual working with React Router by creating a demo application and including all the components.