3

I have a router like:

<Router history={browserHistory}>
        <Route path="/" component={App}>
        <IndexRoute component={Login} />
        <Route path="/#!login" component={Login} />
        <Route path="/#!first" component={First} />
        <Route path="/#!second" component={Second} />
        </Route>
  </Router>

and I have a header component like :

class Header extends Component {
  render() {
    return (
      <div>
        <h3><Link to="/#!first">First</Link></h3>
        <h3><Link to="/#!second">Second</Link></h3>
      </div>
    )
  }
}

export default Header

Now when I click First in my headers it does not redirect me to that link..

How can I implement hash based routing in react and redux

varad
  • 7,309
  • 20
  • 60
  • 112

1 Answers1

1

Use hashHistory, and let all your routes should be aware of your history. Do this instead:

import { hashHistory } from 'react-router';

<Router history={hashHistory}>
    <Route path="/" component={App}>
        <IndexRoute component={Login} />
        <Route path="login" component={Login} />
        <Route path="first" component={First} />
        <Route path="second" component={Second} />
    </Route>
</Router>

Link it like this:

<Link to="/first">First</Link>

And let the router do the rest.

Radosław Miernik
  • 4,004
  • 8
  • 33
  • 36
  • 1
    what if I want hash based routing.. ?? I know this works I have implemented this though but I want to have hash based routing.. – varad Feb 09 '16 at 03:25
  • This worked for me! Thank you so much! I do get some weird crap appended to the end of the page though like a random string it looks like `#/?_k=d74z39` or `#/contact?_k=uxnbcv` – Noitidart Jun 10 '16 at 15:44
  • You can read more about it in [#1967](https://github.com/reactjs/react-router/issues/1967). – Radosław Miernik Jun 11 '16 at 08:16
  • Doesn't work for me: `'react-router' does not contain an export named 'hashHistory'.` on `"react-router": "^4.2.0"` – Jay May 25 '18 at 21:27
  • As you can see in the [CHANGES.md](https://github.com/ReactTraining/react-router/blob/master/CHANGES.md), it was pre v2 back then (`2.0.0` was actually released a day later). – Radosław Miernik May 27 '18 at 16:58