12

Navigation between pages in my navbar is done using an onClick handler, rather than through hrefs. When doing this, I can't either middle click to open in a new tab or right click on the link and select open in new tab. I am using react-router-v4.

An example of a route.

<a onClick={() => this.props.history.push('/conference/')}>Conference</a>

Is there a way to allow this to happen in react or should I be using hrefs instead?

Cameron
  • 123
  • 1
  • 1
  • 4

5 Answers5

6

You can use <Link to='/conference'>Conference</Link> to do this.

Rohith Murali
  • 5,551
  • 2
  • 25
  • 26
3

You should use Link from react router

<Link to={`/conference`}>Conference</Link>
Omar Mneimneh
  • 456
  • 5
  • 14
nishant
  • 2,526
  • 1
  • 13
  • 19
0

Is there a reason you're not using the Link component?

topic
  • 372
  • 2
  • 12
0

If for some reason you do want to programmatically link to other pages and NOT use you can use this method to enable cmd/ctrl clicking.

Tl;dr when ctrl/cmd is pressed put a flag on the state, then use it to select whether to history.push or window.open the new path.

David Avikasis
  • 516
  • 1
  • 5
  • 16
0

Worked for me:

window.open('/conference', '_blank')
german-st
  • 1
  • 2