Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Installation
3.
Routing with Reach router
4.
Frequently asked questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Reach Router

Introduction

Reach Router is a small, simple router for React that borrows from React Router. Reach Router has a small footprint, supports only simple route patterns by design, and has robust accessibility features. The reach router is similar to the react-router. In fact, Ryan Florence is co-creator of both react as well as reach router.

Now the react and reach router developers have decided to merge both the libraries in react router v6. Reach router v2 and react router v6 are the same.

In this blog, we will discuss installation and some basic routing that we can achieve using reach router.

Also See, Dropdown in React JS

Installation

Installing the Reach router is straightforward. To install the reach router just type the following command in the terminal

npm install @reach/router


Once you have installed the reach router, the next thing is to import it to your project.

To import the reach router, just type the following. 

import { Router } from '@reach/router'

One thing to note is Reach Router is compatible with React 15+.

Routing with Reach router

We have installed the reach router and imported it into our project, so let's start setting up a simple web application to understand the reach router.

In this example we will create two component namely home and dashboard and use the reach router to navigate between the components

We will go through the process step by step so firstly we will create both the components:

Home 

const Home = () => {
  return (
    <div>
      <h1>This is the home route</h1>
    </div>
  );
};


Dashboard

const Dashboard = () => {
  return (
    <div>
      <h1>Dashboard</h1>
    </div>
  );
};


Now that we have our components ready, we can start routing. To “switch” between the two components, we need links to point to each component and a Router component of which these two components will be children. We will start with the link. For routing, we need to use a Link component which will be imported from Reach Router. Each Link component will have a to attribute, which is the component's path (we will define this path soon). Let’s add our links.

import {Link } from '@reach/router';
function App() {
  return (
    <>
      <div style={{ display: "flex", justifyContent: "space-evenly" }}>
        <Link to="/">Home</Link>
        <Link to="dashboard">Dashboard</Link>
      </div>
    </>
  );
}


As you can see, the path to the home component is “/” which means the home component will be rendered when the user is on the root path.


Now that we have the links, let us import the Router component and use it.

import { Router,Link } from '@reach/router';
function App() {
  return (
    <>
      <div style={{ display: "flex", justifyContent: "space-evenly" }}>
        <Link to="/">Home</Link>
        <Link to="dashboard">Dashboard</Link>
      </div>
      <Router>
        <Home path="/" />
        <Dashboard path="dashboard" />
      </Router>
    </>
  );
}


We have written all the code in a single file index.js once you have merged all the components the index.js should be like this

import React from "react";
import ReactDOM from "react-dom";
import { Router,Link } from '@reach/router';


const Home = () => {
  return (
    <div>
      <h1>This is the home route</h1>
    </div>
  );
};

const Dashboard = () => {
  return (
    <div>
      <h1>Dashboard</h1>
    </div>
  );
};

function App() {
  return (
    <>
      <div style={{ display: "flex", justifyContent: "space-evenly" }}>
        <Link to="/">Home</Link>
        <Link to="dashboard">Dashboard</Link>
      </div>
      <Router>
        <Home path="/" />
        <Dashboard path="dashboard" />
      </Router>
    </>
  );
}

ReactDOM.render(<App/>, document.getElementById('root'));




The home page that we will get from the above code would look like this

Once you click on the dashboard link the website take you to the dashboard

This was a small basic demo to create a webapp with reach router we have created some basic components and liked them with reach router.

Frequently asked questions

  1. What is Reach Router used for?
    Reach router are used to define multiple routers in a React-based web application. When a user types a specific URL into their browser, and that URL path matches one of the 'routes' and the user is routed to that Route.
     
  2. What are other libraries like Reach router.
    React router is a similar package to Reach router which is used for routing.

Key Takeaways

In this article we discussed about reach router with a basic implementation of a web app. We created a web app with basic components and used reach router for routine the links.

You may check out Full Stack Web Development Course — Node.js + HTML/CSS/JS on Coding Ninjas

If you are preparing for the next interview, check out the blogs 15 Most Frequently Asked React JS Interview Questions and 10 Best ReactJS Interview Questions. And if you are a beginner, check out the Top 5 skills to learn before you start with ReactJs to know the prerequisites to learn React.

If you are preparing for your DSA interviews then , Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and give you an overview of student interview experience in various product-based companies.

Happy learning!

Live masterclass