React Router - open link on new tab and redirect to homepage


Jonka 33

Using React Router 4.2

My attempt is to open a new tab when the nav link is clicked while redirecting it to the website home page.

ie: Navigation Bar: Click Policy

enter image description here

Even though the code below has the above requirements: is this a viable workaround? Aims to learn best practices on Routes.js.

 //Routes.js
 import HandbookDoc from './policies.pdf'
 ...

 <Route 
   path="/registration/policies" 
   component={() => window.open(`${HandbookDoc}`,'_blank').then(window.location= '/')} 
 />

....

 //Navigation.js (using react-router-bootstrap)
  <NavDropdown eventKey={3} id="formId" title="Registration">
     <LinkContainer to="/registration/financial-aid">
        <MenuItem eventKey={3.1}>Financial Aid</MenuItem>
      </LinkContainer>
      <LinkContainer to="/registration/policies">
        <MenuItem eventKey={3.2}>Policies</MenuItem>
      </LinkContainer>
  </NavDropdown>
Jackief

You don't need to create a new route for what you want to achieve. You can add a onClickhandler like this MenuItem:

  <NavDropdown eventKey={3} id="formId" title="Registration">
     <LinkContainer to="/registration/financial-aid">
        <MenuItem eventKey={3.1}>Financial Aid</MenuItem>
      </LinkContainer>
      <LinkContainer 
        to="/">
        <MenuItem onClick={this.handlePoliciesClick} eventKey={3.2}>Policies</MenuItem>
      </LinkContainer>
  </NavDropdown>

Then add the handler in the same component:

handlePoliciesClick = () => {
  window.open(HandbookDoc, '_blank');
}

Remember to import yours HandbookDoc.

Related


React Router - open link on new tab and redirect to homepage

Jonka 33 Using React Router 4.2 My attempt is to open a new tab when the nav link is clicked while redirecting it to the website home page. ie: Navigation Bar: Click Policy Even though the code below has the above requirements: is this a viable workaround? Aim

React Router - open link on new tab and redirect to homepage

Jonka 33 Using React Router 4.2 My attempt is to open a new tab when the nav link is clicked while redirecting it to the website home page. ie: Navigation Bar: Click Policy Even though the code below has the above requirements: is this a viable workaround? Aim

React Router - open link on new tab and redirect to homepage

Jonka 33 Using React Router 4.2 My attempt is to open a new tab when the nav link is clicked while redirecting it to the website home page. ie: Navigation Bar: Click Policy Even though the code below has the above requirements: is this a viable workaround? Aim

React-Router open link in new tab

Mike: Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="f

React-Router open link in new tab

Mike Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="fo

React-Router open link in new tab

Mike: Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="f

React-Router open link in new tab

Mike Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="fo

React-Router open link in new tab

Mike: Is there a way to make React Router open links in new tabs? I tried it and it didn't work. <Link to="chart" target="_blank" query={{test: this.props.test}} >Test</Link> It can be blurred by adding something similar to what I have above to the onClick="f

Correct javascript redirect works with "open link in new tab"

Marco Prince Is there a way to create links using JavaScript exactly like <a>markup ? In other words, when you click it normally, it opens in the current window, and when you right click, it says "Open link in new tab" under options. If you click on it, it doe

Open link in new tab

no mice here I need to open a link <a>in a new tab/window 's tab, do something there and then close it. html code: <body> <a id="uniqueid" href="somelink">I need to open this in a new tab</a> </body> python code: from selenium import webdriver driver = w

Reload page with react-router redirect to homepage

mattxml When I try to reload the subpage app, it redirects me to the main page. When I enter a browser subpage (eg: ) /aboutthe application redirects me to the main page. This is my router component in App.js. PS I am using arrow functions in React. <Router>

React Router browserHistory.push opens link in new tab

Kousha can be <link>used as <Link to="route" target="_blank"> Open the link in a new tab. But can it be used to browserHistory.pushopen a link in a new tab? Ursus React-router is built on the browser's History API. browserHistory.pushcall method.pushState() F

Can't open new tab in react, add localhost:3000 on link?

gpbaculio I've used this link from React Router Doms: <Link target="_blank" to={"www.mylink.com"} >mylink </Link> But this will also use href: open new tab to http://localhost:3000/www.mylink.com . <a href={'www.mylink.com'} target="_blank" > mylink </a> Sam

Can't open new tab in react, add localhost:3000 on link?

gpbaculio I've used this link from React Router Doms: <Link target="_blank" to={"www.mylink.com"} >mylink </Link> But this will also use href: open new tab to http://localhost:3000/www.mylink.com . <a href={'www.mylink.com'} target="_blank" > mylink </a> Sam

Can't open new tab in react, add localhost:3000 on link?

gpbaculio I've used this link from React Router Doms: <Link target="_blank" to={"www.mylink.com"} >mylink </Link> But this will also use href: open new tab to http://localhost:3000/www.mylink.com . <a href={'www.mylink.com'} target="_blank" > mylink </a> Sam

Can't open new tab in react, add localhost:3000 on link?

gpbaculio I've used this link from React Router Doms: <Link target="_blank" to={"www.mylink.com"} >mylink </Link> But this will also use href: open new tab to http://localhost:3000/www.mylink.com . <a href={'www.mylink.com'} target="_blank" > mylink </a> Sam

Can't open new tab in react, add localhost:3000 on link?

gpbaculio I've used this link from React Router Doms: <Link target="_blank" to={"www.mylink.com"} >mylink </Link> But this will also use href: open new tab to http://localhost:3000/www.mylink.com . <a href={'www.mylink.com'} target="_blank" > mylink </a> Sam

Can't open new tab in react, add localhost:3000 on link?

gpbaculio I've used this link from React Router Doms: <Link target="_blank" to={"www.mylink.com"} >mylink </Link> But this will also use href: open new tab to http://localhost:3000/www.mylink.com . <a href={'www.mylink.com'} target="_blank" > mylink </a> Sam

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

pandoc: open link in new tab

singles According to this answer , it should be possible to use the following syntax [link](url){:target="_blank"} When used, links can be opened in a new tab pandoc. However, this option does not seem to be supported as it is not recognized when converting t

Button to open link in new tab

Alex Hawking I'm building a website and I want to open a new tab at a specific address when a button is clicked. Here is the html of my button: <button id="AsDownload" onclick="AsDownload();">Download</button> Here is my javascript: function AsDownload(){

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

pandoc: open link in new tab

singles According to this answer , it should be possible to use the following syntax [link](url){:target="_blank"} When used, links can be opened in a new tab pandoc. However, this option does not seem to be supported as it is not recognized when converting t

Button to open link in new tab

Alex Hawking I'm building a website and I want to open a new tab at a specific address when a button is clicked. Here is the html of my button: <button id="AsDownload" onclick="AsDownload();">Download</button> Here is my javascript: function AsDownload(){

phpBB open link in new tab

Adam I have a phpBB board 3 with prosilver style. When the user clicks the link in the post, the browser will open it in the same tab. Can I change it so that the browser opens the link in a new tab or window? Alexandra Dunn To complete Steve's answer, if you

Open link in new tab or window

Rene Is it possible to open a link a hrefin a new tab instead of the same tab ? <a href="http://your_url_here.html">Link</a> Nason You should add target="_blank"and in the anchor tag rel="noopener noreferrer". E.g: <a target="_blank" rel="noopener noreferrer"

pandoc: open link in new tab

singles According to this answer , it should be possible to use the following syntax [link](url){:target="_blank"} When used, links can be opened in a new tab pandoc. However, this option does not seem to be supported as it is not recognized when converting t

Button to open link in new tab

Alex Hawking I'm building a website and I want to open a new tab at a specific address when a button is clicked. Here is the html of my button: <button id="AsDownload" onclick="AsDownload();">Download</button> Here is my javascript: function AsDownload(){