39

I'm using react-router for my routing and I use the hashHistory option so that I could refresh the page from the browser or specify a url of one of my existing routes and land on the right page. It works fine but I see the hash in the url like this: http://localhost/#/login?_k=ya6z6i

This is my routing configuration:

ReactDOM.render((
 <Router history={hashHistory}>
    <Route path='/' component={MasterPage}>
      <IndexRoute component={LoginPage} />
      <Route path='/search' component={SearchPage} />
      <Route path='/login' component={LoginPage} />
      <Route path='/payment' component={PaymentPage} />
    </Route>
  </Router>),
    document.getElementById('app-container'));
Dennis Nerush
  • 5,473
  • 5
  • 25
  • 33

5 Answers5

39

Did you try the browserHistory option ? You will be able also to refresh the page from the browser or specify a url of one of existing routes and land on the right page.

import { Router, Route, browserHistory } from 'react-router';

ReactDOM.render((
 <Router history={browserHistory}>
    <Route path='/' component={MasterPage}>
      <IndexRoute component={LoginPage} />
      <Route path='/search' component={SearchPage} />
      <Route path='/login' component={LoginPage} />
      <Route path='/payment' component={PaymentPage} />
    </Route>
  </Router>),
    document.getElementById('app-container'));

Moreover hashHistory is not for production use considering the react-router github doc.

https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#browserhistory

Should I use hashHistory?

Hash history works without configuring your server, so if you're just getting started, go ahead and use it. But, we don't recommend using it in production, every web app should aspire to use browserHistory

skwidbreth
  • 7,888
  • 11
  • 58
  • 105
Aaleks
  • 4,283
  • 5
  • 31
  • 39
  • 2
    i tried it and the hash disappeared. However i cannot land on the same page after browser refresh – Dennis Nerush Feb 03 '16 at 19:05
  • Do you use a server (node.js) to serve your files ? – Aaleks Feb 03 '16 at 19:13
  • 2
    It should work if the first time you make a request to the server let's say at mysite.com you serve an index.html file when you click on a button that invoke the router mysite.com/page1 it will show the page using react router on the client side. But when you it refresh it ask the server the URL mysite.com/page1 but if you serve for every request the index.html file with a pattern like * on every request i serve index.html you should see the right content because mysite.com/page1 -> give index.html but the react router see /page1 and do the job like you clicked yourself again on page1 button. – Aaleks Feb 04 '16 at 14:54
  • `browserHistory` need server side rendering. so I use `hashHistory` in production, because I won't need server side rendering in my project. – AmerllicA Nov 27 '17 at 16:14
  • If you want to use `react-router-dom` with `history`, use: `import createHistory from 'history/createBrowserHistory';` – Juan Mar 12 '18 at 22:15
  • In react-router-dom v6 you can use BrowserRouter for remove # – Piyush Jain Sep 13 '22 at 06:27
12

i am new to react but i have used BrowserRouter and it works fine :-

    import React from "react";
    import ReactDOM from "react-dom";
    import { BrowserRouter, Route, Switch } from "react-router-dom";

ReactDOM.render(
  <BrowserRouter >
    <Switch>
      {indexRoutes.map((prop, key) => {
        return <Route to={prop.path} component={prop.component} key={key} />;
      })}
    </Switch>
  </BrowserRouter>,
  document.getElementById("root")
);
Eassa Nassar
  • 700
  • 9
  • 15
5

I believe it was mentioned above by Dennis Nerush, you need to use {browserHistory} instead of {hashHistory}, however for the same page rendering to work you need to configure your server a bit to allow for this.

Depending on where you are hosted or what server you are using there are a few ways to do this

For Apache you must add the following to the .htaccess (or create .htaccess and place it in the root folder of your website):

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

For Node.js/ Express you need to add the following code:

app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname, 'path/to/your/index.html'), function(err) {
    if (err) {
      res.status(500).send(err)
    }
  })
})

for Nginx servers you need to add the following to the Nginx.conf file

location / {
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.html break;
  }
}

For Amplify you need to go to Rewrites & Redirects and add a new rule that with the following information (Note I have only used this on AWS):

Source address: </^[^.]+$|\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$)([^.]+$)/>
Target address: /index.html
Type: 200

Here are a few links if you want to do some more research on the subject.

https://www.andreasreiterer.at/fix-browserrouter-on-apache/#comment-186 (Specifically for Apache)

https://ui.dev/react-router-cannot-get-url-refresh/ (Multiple methods for different servers or no server which I did not add above)

React Router DOM not working correctly on Amplify Console AWS (Use this one for AWS and Amplify)

Pieter Burger
  • 111
  • 1
  • 7
3

You will need to import browserHistory from react-router
and pass it to the <Router /> in order to remove the hash from the URL

Ex:

import { browserHistory } from 'react-router';

<Router history={browserHistory}>
 //Place your route here
</Router>
Umair Ahmed
  • 8,337
  • 4
  • 29
  • 37
0

Try this:

// v1.x
import createBrowserHistory from 'history/lib/createBrowserHistory'
<Router history={createBrowserHistory()} />

// v2.0.0
import { browserHistory } from 'react-router'
<Router history={browserHistory} />


const history = createHashHistory({ queryKey: false });
<Router history={history}>
</Router>

https://github.com/reactjs/react-router/blob/master/upgrade-guides/v2.0.0.md#using-custom-histories

https://github.com/reactjs/react-router/blob/8ef625e8fa7c41dda0f3d1d68a10b74bd93a4101/docs/guides/ba...

Albert Olivé Corbella
  • 4,061
  • 7
  • 48
  • 66
  • have guess why I got this error without even using react-router? just react and react-dom – Jjang Sep 20 '16 at 19:48
  • What about 3.x? `You have provided a history object created with history v2.x or earlier. This version of React Router is only compatible with v3 history objects. Please upgrade to history v3.x.` – holms Dec 21 '16 at 23:27
  • ok fixed with this `import { createHistory } from 'history'; // you need to install this package let history = createHistory();` – holms Dec 22 '16 at 00:06