React-Router open Link in new tab

前端 未结 11 592
傲寒
傲寒 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:12

    Starting with react_router 1.0, the props will be passed onto the anchor tag. You can directly use target="_blank". Discussed here: https://github.com/ReactTraining/react-router/issues/2188

    0 讨论(0)
  • 2020-12-08 02:15

    We can use the following options:-

     // first option is:-
        <Link to="myRoute" params={myParams} target="_blank">
    
     // second option is:-
        var href = this.props.history.createHref('myRoute', myParams);
        <a href={href} target="_blank">
    
     //third option is:-
        var href = '/myRoute/' + myParams.foo + '/' + myParams.bar;
        <a href={href} target="_blank">
    

    We can use either of three option to open in new tab by react routing.

    0 讨论(0)
  • 2020-12-08 02:21

    this works fine for me

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

    target="_blank" is enough to open in a new tab, when you are using react-router

    eg: <Link to={/admin/posts/error-post-list/${this.props.errorDate}} target="_blank"> View Details </Link>

    0 讨论(0)
  • 2020-12-08 02:23

    In my case, I am using another function.

    Function

     function openTab() {
        window.open('https://play.google.com/store/apps/details?id=com.drishya');
      }
    
      <Link onClick={openTab}></Link>
    
    0 讨论(0)
提交回复
热议问题