10

I'm converting some existing code to use React Router.

The code currently uses <a href="#" ...>, which I am changing to <Link to=??>.

My question is: What should I use for the "to" parameter? If I use to="#", the application routes to "/", which is not what I want.

It works if I use the current route name, but the whole idea of href="#" is that the code doesn't have to know how it is accessed.

I am using React Router 2 with history=browserHistory.

Hexie
  • 3,955
  • 6
  • 32
  • 55
Mark L
  • 400
  • 1
  • 3
  • 11
  • Does this answer your question? [Using anchor tags in react-router 4](https://stackoverflow.com/questions/48223566/using-anchor-tags-in-react-router-4) – Sam Apostel Jul 01 '20 at 13:53

7 Answers7

4

Here are a few solutions that worked for me:

  • <Link to={{}}>
  • <Link to={{ search: '' }}>
    • In my specific case, I wanted to stay on the same page but wipe the search params, which is what this does
  • <Link to={window.location.pathname}>
  • <Link to="#">
    • This seemed to work fine for me, maybe because I'm using react-router v6

What didn't work for me:

  • <Link to={this.props.route.path}
    • I'm using functional components, so I don't have any this.props. Maybe there's another way in the react-router API to get the path.
  • <Link to=".">
    • This linked to / for me
bmaupin
  • 14,427
  • 5
  • 89
  • 94
3

For me this was the solution:

I used the npm module react-router-hash-link. It is quite easy to use. Docs here

import { HashLink as Link } from 'react-router-hash-link'; <Link smooth to="/#services">Services</Link>

And wrap your <App> component in <HashRouter> from npm module react-router-dom

stackoverflow answer

Seagull
  • 1,063
  • 1
  • 11
  • 18
1

I think you could try something more or less like that:

<Link to={window.location.pathname} hash="/#">HASH</Link>

See there : https://github.com/reactjs/react-router/blob/master/docs/API.md#hash

Damien Leroux
  • 11,177
  • 7
  • 43
  • 57
  • This does not work, but the hint to use a props parameter was a good one. I'll give the solution in a formal "answer". Thanks. – Mark L Mar 14 '16 at 11:20
1

This works because "this.props.route.path" is the route to the current page:

<Link to={this.props.route.path} ...

Note that if you have nested React components, you have to pass "this.path" down from the outer components, as described at https://facebook.github.io/react/docs/transferring-props.html

Mark L
  • 400
  • 1
  • 3
  • 11
1
<Link to='#' />

this works but it will still stack your history

Acid Coder
  • 2,047
  • 15
  • 21
0

It works for me:

<Link className="dropdown-item" to="javascript:void()">
   Link Title
</Link>
Chung Nguyen
  • 481
  • 4
  • 6
-1

If you need to go to a specified section of your "/" path (even from another path), you can make it work with the anchor tag as well, like this:

<a href="./#your-section-id">Go to Section</a>

Hope this helps.

GTom
  • 109
  • 3
  • this does not answer the question asked. this: https://stackoverflow.com/questions/48223566/using-anchor-tags-in-react-router-4 does – Sam Apostel Jul 01 '20 at 13:52