Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Router open Link in new tab

Is there a way to get React Router to open a link in new tab? I tried this and it did not work.

<Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link>

It's possible to fluff it by adding something like onClick="foo" to the Link like what I have above, but there would be a console error.

Thanks.

like image 337
mcd Avatar asked Oct 14 '22 04:10

mcd


People also ask

How do you make a link open in a new tab in ReactJS?

To open a link in a new tab in React, use the <a> element and set its target prop to _blank , e.g. <a href="https://google.com" target="_blank" rel="noopener noreferrer"></a> . The _blank value means that the resource is loaded into a new tab.

How redirect external URL in react?

Use the window. location. replace() method to redirect to an external url in React, e.g. window. location.

How do you use target attribute in react?

To set the target prop of an element to _blank in React, use an anchor element and set the rel prop, e.g. <a href="https://example.com" target="_blank" rel="noopener noreferrer"> . The _blank value means that the resource is loaded into a new tab.


1 Answers

Since React Router version 5.0.1, you can use:

<Link to="route" target="_blank" rel="noopener noreferrer" />
like image 210
hydRAnger Avatar answered Oct 16 '22 18:10

hydRAnger