I have a single page in my application with several sections which can be scrolled. I want to use React Router to create links to these sections. I want them to be real links without the #. Exactly what they have done in the react-router documentation (https://reacttraining.com/react-router/web/guides/quick-start). In there, the sidebar links doesn't use anchor tags like #about. Instead, they use regular links which get mapped to the anchor tags. How can we do this?
Add a comment
|
1 Answer
There are different types of Routers included with react-router, if you want the html5 push state version with no anchor tags, then you would use BrowserRouter.
import { BrowserRouter } from 'react-router-dom'
<BrowserRouter
basename={optionalString}
forceRefresh={optionalBool}
getUserConfirmation={optionalFunc}
keyLength={optionalNumber}>
<App/>
</BrowserRouter>
--
<BrowserRouter basename="/calendar"/>
<Link to="/today"/> // renders <a href="/calendar/today">
If you wanted to use the anchor tag routing, then you would use HashRouter
2 Comments
THpubs
In your example, does
<a href="/calendar/today"> link to an anchor tag #today. HashRouter does add a # to the url right?Austin Greco
yes, if you see in the docs for HashRouter the example it shows that a
<Link> will render with the # -- <Link to="/today"/> // renders <a href="#/calendar/today"