How to use React Router 6, React Link, and nested Links?

Routing from one page to another page on a web application is a very common feature, it helps users to navigate from one page to another page.

React Router 6 is used in React js application for routing purposes. Routing from one page to another is very fast in react js and React Router 6 makes it easier to implement.

First, we need to install react-router and react-router-dom for that we will run this command in a terminal inside the project location, also install history which helps to track the routing history.

npm install react-router@next react-router-dom@next
npm install history

Firstly we need to create a file named Pages.js, where we write functions for all the pages we need for navigation.

We are also importing Link from react-router-dom so that we can navigate through the pages by clicking links.

The basic requirement is, we have a home page with two links for about and contact pages, and when we click on about it will show two more links for information and address page.

Contact page is just displaying message “Contact Page”.

These Information and address pages are opening through the nested links which are related to the about function or about page.

Firstly let’s look into the Pages.js file

import {Link} from 'react-router-dom';
import {useLocation} from 'react-router-dom';
import { Outlet } from 'react-router';
 export function Home(){
    return (
                <Link to="about">About</Link><br/>
                <Link to="contact">Contact</Link>
 export function About(){
     let currentLocaiton = useLocation();
    return (
            My Current URL Location is {currentLocaiton.pathname}<br/>
            <Link to="info">Information</Link><br/>
            <Link to="address">Address</Link>

 export function Contact(){
    return (
export function Info(){
    return (
export function Address(){
    return (
const Pages =()=>{
export default Pages;

Here useLocation() is for showing the current URL path or location of page.

Outlet is for showing the nested pages data.

Inside App.js file we are using the nested routes and we are defining the information and address pages inside the about page, and that’s why Outlet helps to detect the nested data of pages. You will understand it better once you see the below file which is App.js

import './App.css';
import {Home,About,Contact,Info,Address} from './Pages';
import Error from './Error';
import {Routes,Route} from 'react-router-dom';

function App() {
  return (
    <Route path="/" element={<Home/>}/>
    <Route path="/about" element={<About/>}>
      <Route path="/about/info" element={<Info/>}/>
      <Route path="/about/address" element={<Address/>}/>
    <Route path="/contact" element={<Contact/>}/>
    <Route path="*" element={<Error/>}/>

export default App;

So now you can easily understand this functionality of routing and linking of pages.

One more file is there which is Error.js, which we have created in case of URL is invalid if we try to access the URL with a random name.

Here is the Error.js file

const Error =()=>{
    return <h2>Page not found</h2>
export default Error;

Let’s see the output screens

This is the home page, now if we click on about page next screen will display

Finally, if we click on information or address, the Outlet will display data from the specific page or function.

This is very easy to understand, once you practice this you will be an expert in react routing.

If you have any questions feel free to comment ill reply 🙂

Related Posts

angular vs react-min

What is the difference between Angular & React JS, Which one you should choose?

Angular & React JS both are use for Frontend Development, To make Single page applications or Web Apps. Here is a quick difference between both of them….

react js icon

What is Redux? What is action, reducer and store in Redux

In this blog we will discus about the redux, what is Redux and how you can use it in React application. What is Redux? Redux is a…

react js icon

What is useEffect Hook? How to Fetch data from API using useEffect

useEffect hook is used to manage side effects that are not related to component rendering. Mostly useEffect hook is used for managing console logs, animations, loading data,…

react js icon

What is useReducer Hook? What is the difference between useState and useReducer Hooks?

useReudcer hook takes the current state and returns a new state, it is used for state management. With useReducer, we can directly call the function inside the…

react js icon

Creating React JS App without using npx create-react-app command

It’s easy to create a react application by just running npx create-react-app project_name command, do you know what are the basic requirements for creating a react application…

react js icon

What is the useState() hooks in React JS | How to use useState() hook in React JS

Hooks are very important in react js and the most common hook is the useState Hook. In this blog, we will look into the functionality of this…

Leave a Reply

Your email address will not be published. Required fields are marked *