reactjsreact-router-domreact-bootstrapreact-router-bootstrap

How to avoid hash sign in URL, using react-router-dom and react-router-bootstrap?


I'm using <Route /> from react-router-dom for routing and Nav + LinkContainer from react-bootstrap + react-router-bootstrap for navigation:

//...
  <Route path="/shop/" component={Shop} />
//...
  <NavItem caption="Home" path="/" />
  <NavItem caption="Shop" path="/shop" />
//...
    
// with a helper component: 
const NavItem = (props) => {
  return (
    <LinkContainer to={props.path} > 
      <Nav.Link> {props.caption} </Nav.Link> 
    </LinkContainer>
  )
}

This creates an URL with a hash sign in it, e.g.:

http://localhost:3000/#/shop

Where does this come from, what is it good for? Everything is working fine anyway, but this hash sign isn't smart, I think. Can I avoid it?


Solution

  • You can use

    BrowserRouter

    Instead of

    HashRouter

    which prevents the showing # on URL.

    import {
      BrowserRouter as Router,
      ...
    }
    
    instead of
    
    import {
      HashRouter as Router,
      ...
    }