Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between HashRouter and BrowserRouter in React?

Tags:

reactjs

People also ask

What is HashRouter in react JS?

A <Router> that uses the hash portion of the URL (i.e. window. location. hash ) to keep your UI in sync with the URL.

What is the use of BrowserRouter in react?

React Router is a standard library for routing in React. It enables navigation between views from different components in a React application, allows the browser URL to be changed, and keeps the UI in sync with the URL.

What is the BrowserRouter /> component?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.

What is the default value for key length in BrowserRouter?

keyLength: number The length of location. key . Defaults to 6.


BrowserRouter

It uses history API, i.e. it's unavailable for legacy browsers (IE 9 and lower and contemporaries). Client-side React application is able to maintain clean routes like example.com/react/route but needs to be backed by web server. Usually this means that web server should be configured for single-page application, i.e. same index.html is served for /react/route path or any other route on server side. On client side, window.location.pathname is parsed by React router. React router renders a component that it was configured to render for /react/route.

Additionally, the setup may involve server-side rendering, index.html may contain rendered components or data that are specific to current route.

HashRouter

It uses URL hash, it puts no limitations on supported browsers or web server. Server-side routing is independent from client-side routing.

Backward-compatible single-page application can use it as example.com/#/react/route. The setup cannot be backed up by server-side rendering because it's / path that is served on server side, #/react/route URL hash cannot be read from server side. On client side, window.location.hash is parsed by React router. React router renders a component that it was configured to render for /react/route, similarly to BrowserRouter.

Most importantly, HashRouter use cases aren't limited to SPA. A website may have legacy or search engine-friendly server-side routing, while React application may be a widget that maintains its state in URL like example.com/server/side/route#/react/route. Some page that contains React application is served on server side for /server/side/route, then on client side React router renders a component that it was configured to render for /react/route, similarly to previous scenario.


SERVER SIDE: HashRouter uses a hash symbol in the URL, which has the effect of all subsequent URL path content being ignored in the server request (ie you send "www.mywebsite.com/#/person/john" the server gets "www.mywebsite.com". As a result the server will return the pre # URL response, and then the post # path will be handled by parsed by your client side react application.

CLIENT SIDE: BrowserRouter will not append the # symbol to your URL, however will create issues when you try to link to a page or reload a page. If the explicit route exists in your client react app, but not on your server, reloading and linking(anything that hits the server directly) will return 404 not found errors.


Refreshing the page causes the browser to send a GET request to the server using the current route. The # was used to prevent us from sending that GET request. We use the BrowserRouter because we want the GET request to be sent to the server. In order to render the router on the server, we need a location — we need the route. This route will be used on the server to tell the router what to render. The BrowserRouter is used when you want to render routes isomorphically.


Both BrowserRouter and HashRouter components were introduced in React Router ver.4 as subclasses of Router class. Simply, BrowserRouter syncs the UI with the current URL in your browser, This is done by the means of HTML-5 History API. On the other hand, HashRouter uses the Hash part of your URL to sync.


"Use Cases"

HashRouter: When we have small client side applications which doesn't need backend we can use HashRouter because when we use hashes in the URL/location bar browser doesn't make a server request.

BrowserRouter: When we have big production-ready applications which serve backend, it is recommended to use <BrowserRouter>.

Reference by book: Learning React: Functional Web Development with React and Redux By Alex Banks, Eve Porcello


The main use case scenario for choosing "hash router" over "browser router" is on production environment. Let's say we have this url "www.example.com/Example". In this case, some servers tend to search a folder by a name "Example" and ultimately return 404 as we have single page application, index.html. So, to avoid such confusion we use "www.example.com/#/Example". That is where hash router shines.

resource: https://www.techblogsnews.com/post/browser-router-vs-hash-router-in-react-js


One more use case i want to add. While using BrowserRouter or Router, it will work fine on our node server. Because its understand client routing (Preconfigured).

But while we deploy our build React app on Apache server(just say PHP, on GoDaddy), then routing will not work as expected. It will land into 404, for that we need to configure .htaccess file. After that also for me each click/url, its sending request to server.

In that case better we use HASH Routing (#). # we use this on our html page, for traversing in HTML content and it will not lead to server request.

In above scenario we can use HashRouting, that is all url that present after #, we can put our routing rules to work as SPA.