React-router: set target='_blank'

Created on 10 Jul 2015  路  5Comments  路  Source: ReactTraining/react-router

Is there an easy way to set target='_blank' using the Link helper? It seems that this attribute does not get rendered.

The only way I've been able to set this attribute on a link was to create an anchor tag myself and use makeHref to set the href attribute.

feature

Most helpful comment

fyi:https://mathiasbynens.github.io/rel-noopener/ (a bug related to target="_blank")

All 5 comments

+1, this would be incredibly useful for my work.

It does get rendered since <Link> transfers all props to <a>. However, <Link> needs to handle navigation using JS and prevent browser navigation. I guess there's an argument for making <Link> respect the target attribute, but the direction has been to keep <Link> minimal and sparse in features.

The only way I've been able to set this attribute on a link was to create an anchor tag myself and use makeHref to set the href attribute.

Another solution could be to use the onClick handler:

<Link to="route" target="_blank" onClick={(event) => {
  event.preventDefault();
  window.open(this.makeHref("route"));
}} />

seems like a good idea that if there's a target=_blank to skip our work and let the browser do its thing, then you still get isActive functionality

FYI this feature is supported in 1.0!

fyi:https://mathiasbynens.github.io/rel-noopener/ (a bug related to target="_blank")

Was this page helpful?
0 / 5 - 0 ratings