Sunday, September 22, 2024
Google search engine
HomeLanguagesReactJS Types of Routers

ReactJS Types of Routers

In this article, we will discuss the types of Routers that are provided with react-router-dom. Till now we only used BrowserRouter as it is the most widely used React router but there are some other types of router provided with this package

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: 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: 

JavaScript




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: 

JavaScript




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: 

JavaScript




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:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments