Open In App

React JS Types of Routers

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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: 

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: 

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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads