React JS Types of Routers - GeeksforGeeks (2024)

Last Updated : 28 Nov, 2023

Summarize

Comments

Improve

Till now we only used BrowserRouter as it is the most widely used React router but there are some other types of routers provided with this package

Table of Content

  • React JS Types of Routers
  • Memory Router
  • Browser Router
  • Hash Router

React JS Types of Routers :

On the basis of the part of the URL that the router will use to track the content that the user is trying to view, React Router provides three different kinds of routers:

  • Memory Router
  • Browser Router
  • Hash Router

Memory Router:

The memory router keeps the URL changes in memory not in the user browsers. It keeps the history of the URL in memory and it does not read or write to the address bar so the user can not use the browser’s back button as well as the forward button. It doesn’t change the URL in your browser. It is very useful for testing and non-browser environments like React Native.

Syntax:

import { MemoryRouter as Router } from 'react-router-dom';

Example:This example demonstrates the use of MemoryRouter.

JavaScript

// Filename - App.js

import React, { Component } from "react";

import {

MemoryRouter as Router,

Route,

Link,

Switch,

} from "react-router-dom";

import Home from "./component/home";

import About from "./component/about";

import Contact from "./component/contact";

import "./App.css";

class App extends Component {

render() {

return (

<Router>

<div className="App">

<ul className="App-header">

<li>

<Link to="/">Home</Link>

</li>

<li>

<Link to="/about">

About Us

</Link>

</li>

<li>

<Link to="/contact">

Contact Us

</Link>

</li>

</ul>

<Switch>

<Route

exact

path="/"

component={Home}

></Route>

<Route

exact

path="/about"

component={About}

></Route>

<Route

exact

path="/contact"

component={Contact}

></Route>

</Switch>

</div>

</Router>

);

}

}

export default App;

Output:

React JS Types of Routers - GeeksforGeeks (1)

Browser Router:

It uses HTML 5 history API (i.e. pushState, replaceState, and popState API) to keep your UI in sync with the URL. It routes as a normal URL in the browser and assumes that the server is handling all the request URL (eg., /, /about) and points to root index.html. It accepts forceRefresh props to support legacy browsers that don’t support HTML 5 pushState API

Syntax:

import { BrowserRouter as Router } from 'react-router-dom';

Example:This example demonstrates the use of BrowserRouter.

JavaScript

// Filename - App.js

import React, { Component } from "react";

import {

BrowserRouter as Router,

Route,

Link,

Switch,

} from "react-router-dom";

import Home from "./component/home";

import About from "./component/about";

import Contact from "./component/contact";

import "./App.css";

class App extends Component {

render() {

return (

<Router>

<div className="App">

<ul className="App-header">

<li>

<Link to="/">Home</Link>

</li>

<li>

<Link to="/about">

About Us

</Link>

</li>

<li>

<Link to="/contact">

Contact Us

</Link>

</li>

</ul>

<Switch>

<Route

exact

path="/"

component={Home}

></Route>

<Route

exact

path="/about"

component={About}

></Route>

<Route

exact

path="/contact"

component={Contact}

></Route>

</Switch>

</div>

</Router>

);

}

}

export default App;

Output:

React JS Types of Routers - GeeksforGeeks (2)

Hash Router:

Hash router uses client-side hash routing. It uses the hash portion of the URL (i.e. window.location.hash) to keep your UI in sync with the URL. The hash portion of the URL won’t be handled by the server, the server will always send the index.html for every request and ignore the hash value. It doesn’t need any configuration in the server to handle routes. It is used to support legacy browsers which usually don’t support HTML pushState API. It is very useful for legacy browsers or you don’t have a server logic to handle the client-side. This route isn’t recommended to be used by the react-router-dom team.

Syntax:

import { HashRouter as Router } from 'react-router-dom';

Example:This example demonstrates the use of HashRouter.

JavaScript

// Filename - App.js

import React, { Component } from "react";

import {

HashRouter as Router,

Route,

Link,

Switch,

} from "react-router-dom";

import Home from "./component/home";

import About from "./component/about";

import Contact from "./component/contact";

import "./App.css";

class App extends Component {

render() {

return (

<Router>

<div className="App">

<ul className="App-header">

<li>

<Link to="/">Home</Link>

</li>

<li>

<Link to="/about">

About Us

</Link>

</li>

<li>

<Link to="/contact">

Contact Us

</Link>

</li>

</ul>

<Switch>

<Route

exact

path="/"

component={Home}

></Route>

<Route

exact

path="/about"

component={About}

></Route>

<Route

exact

path="/contact"

component={Contact}

></Route>

</Switch>

</div>

</Router>

);

}

}

export default App;

Syntax:

React JS Types of Routers - GeeksforGeeks (3)



Please Login to comment...

React JS Types of Routers - GeeksforGeeks (2024)
Top Articles
There’s a New Way to Buy Booming Russian Stocks – ETFs - The Moscow Times
How to invest $10,000: 6 financial experts share their best ideas for maximizing your returns across stocks, bonds, and ETFs right now
Ups Stores Near
Pinellas County Jail Mugshots 2023
Obor Guide Osrs
Nfr Daysheet
PontiacMadeDDG family: mother, father and siblings
Eric Rohan Justin Obituary
Acts 16 Nkjv
Acbl Homeport
Housing Intranet Unt
LeBron James comes out on fire, scores first 16 points for Cavaliers in Game 2 vs. Pacers
MindWare : Customer Reviews : Hocus Pocus Magic Show Kit
UEQ - User Experience Questionnaire: UX Testing schnell und einfach
Craigslist Pets Sac
“In my day, you were butch or you were femme”
Walmart Double Point Days 2022
Wizard Build Season 28
Suffix With Pent Crossword Clue
Golden Abyss - Chapter 5 - Lunar_Angel
Welcome to GradeBook
Curry Ford Accident Today
Sizewise Stat Login
Pjs Obits
Www.craigslist.com Savannah Ga
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
Craigslist Pennsylvania Poconos
Bill Remini Obituary
Crossword Help - Find Missing Letters & Solve Clues
Southwest Flight 238
Dtm Urban Dictionary
From This Corner - Chief Glen Brock: A Shawnee Thinker
Section 408 Allegiant Stadium
897 W Valley Blvd
Mobile crane from the Netherlands, used mobile crane for sale from the Netherlands
Promatch Parts
Dubois County Barter Page
Housing Assistance Rental Assistance Program RAP
Capital Hall 6 Base Layout
Everstart Jump Starter Manual Pdf
Kazwire
Devotion Showtimes Near The Grand 16 - Pier Park
Centimeters to Feet conversion: cm to ft calculator
Best Haircut Shop Near Me
Jigidi Free Jigsaw
Hdmovie2 Sbs
4Chan Zelda Totk
Solving Quadratics All Methods Worksheet Answers
Game Like Tales Of Androgyny
Houston Primary Care Byron Ga
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5726

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.