React-Router open Link in new tab

前端 未结 11 591
傲寒
傲寒 2020-12-08 01:24

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



        
相关标签:
11条回答
  • 2020-12-08 02:04

    I think Link component does not have the props for it.

    You can have alternative way by create a tag and use the makeHref method of Navigation mixin to create your url

    <a target='_blank' href={this.makeHref(routeConsts.CHECK_DOMAIN, {},
       { realm: userStore.getState().realms[0].name })}>
            Share this link to your webmaster
    </a>
    
    0 讨论(0)
  • 2020-12-08 02:06

    For external link simply use an achor in place of Link:

    <a rel="noopener noreferrer" href="http://url.com" target="_blank">Link Here</a>
    
    0 讨论(0)
  • 2020-12-08 02:08

    The simples way is to use 'to' property:

    <Link to="chart" target="_blank" to="http://link2external.page.com" >Test</Link>
    
    0 讨论(0)
  • 2020-12-08 02:11

    In React Router version 5.0.1 and above, you can use:

    <Link to="route" target="_blank" onClick={(event) => {event.preventDefault(); window.open(this.makeHref("route"));}} />
    
    0 讨论(0)
  • 2020-12-08 02:11

    You can use "{}" for the target, then jsx will not cry

    <Link target={"_blank"} to="your-link">Your Link</Link>
    
    0 讨论(0)
  • 2020-12-08 02:11

    To open an url in a new tab, you can use the Link tag as below:

    <Link to="/yourRoute" target="_blank">
        Open YourRoute in a new tab
    </Link>
    

    It's nice to keep in mind that the <Link> element gets translated to an <a> element, and as per react-router-dom docs, you can pass any props you'd like to be on it such as title, id, className, etc.

    0 讨论(0)
提交回复
热议问题