216

I am new to programming which makes things slightly difficult for me to understand if I read the official docs.

I was reading about React Router 4 from here

In this article, the author was talking about <HashRouter> and <BrowserRouter>

This is what he mentioned:

HashRouter basically it uses the hash in the URL to render the component. Since I was building a static one-page website, I needed to use this.

BrowserRouter, it uses HTML5 history API to render the component. The history can be modified via pushState and replaceState. More information can be found here

Now, I don't get the significance and use cases for both, Like what does he mean when he says history can be modified via pushState and replaceState and it uses the hash in the URL to render the component

While the first explanation for BrowserRouter is entirely vague to me, the second explanation about HashRouter also doesn't make sense, like why would someone use Hash (#) in the url to render the component?

3

10 Answers 10

252
+50

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.

2
  • 3
    Another point - if you need anchors navigation on the page (which is location.hash was generally designed for and supposed to work out of the box) it's a bit harder to implement. Mar 6, 2019 at 6:43
  • 3
    @iRohitBhatia BrowserHistory also let you do server-side render as the server has access to the full URL. The server does not have access to the path behind the #. May 29, 2019 at 3:08
81

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 (or 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.

0
21

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.

2
  • 2
    Most important to be noted.
    – C Sharper
    Apr 18, 2021 at 12:09
  • 1
    Are you telling that HashBrowser can be used by SPA(single page application) only, but BrowserRouter can be used by SPA and by multi-page applications ( different pages returned from servers)? Jul 24, 2021 at 1:51
13

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.

12

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.

11

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

8

"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

2
  • 49
    Imho "HashRouter" vs. "BrowserRouter" has nothing to do with "small" vs. "big production-ready" applications. There are neither limits nor performance issues using HashRouter in large production-ready applications. It's all about the specific use case, the requirements and the resulting architecture. Serverless production applications are a real thing.
    – Pawel
    Oct 15, 2019 at 8:52
  • Do you mean by “Small” SPA(single page) applications, and telling that BrowserRouter can be used by multi-page applications? Jul 24, 2021 at 1:56
5

Comment BrowserRouter vs Router with history.push() refers to the article https://www.techiediaries.com/react/react-router-5-4-tutorial-examples/#React_Router_5_Routers_BrowserRouter_vs_HashRouter

“If you are using a dynamic server that can handle dynamic URLs then you need to use the BrowserRouter component but if you are using a server that only serves static files then a HashRouter component is what to be used in this cases”

Limitation of HashRouter re history

I previously misinterpreted a note at the top of HashRouter article in https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/HashRouter.md

As this technique is only intended to support legacy browsers, we encourage you to configure your server to work with BrowserHistory instead.

as discouraging to use HashRouter in general, but authors do not recommend HashRouter only in case if if you need support for location.key or location.state.

0

Another key scenario where both types of routers differ is when deploying your app on a subdirectory, i.e. your app is not hosted at the server root host.com. Let's say you want all your routes to start from host.com/prefix. In this case you will have to use a Hash Router, unless you re-compile your react app again for each different prefix.

-3

Basically if one uses BrowserRouter one can use a url like "soAndSoReactPage/:id"

eg:

with a Route <Route path="soAndSoReactPage/:id"><soAndSoReactComponent/></Route>

now, in the react component "soAndSoReactComponent" the "id" can then be extracted using useParams and thus you can display the page "soAndSoReactComponent" as per the id

such a thing can't be done with HashedRouter,

2
  • 1
    No, it's working for both BrowserRouter and HashRouter. Feb 10, 2021 at 19:22
  • 1
    Hash history cannot push state, it is ignored. But path params are working. Sep 24, 2021 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.