javascript - React Router - Open Link on new Tab and Redirect to Home Page - Stack Overflow

admin2025-04-03  0

Using React Router 4.2

My attempt is to open a new tab upon clicking on a navigation link and at the same time be redirected to the site homepage.

ie: Navigation bar: clicking on Policies

Even though the code bellow behaves as the requirements above: Is this the advisable way to go about it? Aiming to learn best practices here on Routes.js.

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

 <Route 
   path="/registration/policies" 
   ponent={() => 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>

Using React Router 4.2

My attempt is to open a new tab upon clicking on a navigation link and at the same time be redirected to the site homepage.

ie: Navigation bar: clicking on Policies

Even though the code bellow behaves as the requirements above: Is this the advisable way to go about it? Aiming to learn best practices here on Routes.js.

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

 <Route 
   path="/registration/policies" 
   ponent={() => 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>
Share edited Jul 20, 2018 at 3:15 Jonca33 asked Jul 20, 2018 at 2:31 Jonca33Jonca33 3,4937 gold badges28 silver badges39 bronze badges 4
  • Is there any reason for the path /registration/policies to exist here? I would simply add an onClick listener on policies, and just do the stuffs you need there. – Jackyef Commented Jul 20, 2018 at 2:48
  • The path set on routes.js is matching the same passed on navigation.js. How would you go about just setting an onClick on that case? – Jonca33 Commented Jul 20, 2018 at 3:19
  • By adding an onClick handler on MenuItem that contains Policies. In there, you would be able to do window.open(), and browserHistory.push('/') – Jackyef Commented Jul 20, 2018 at 3:23
  • Indeed. Mind posting an answer? I haven't played much with browserHistory.push() – Jonca33 Commented Jul 20, 2018 at 3:41
Add a ment  | 

1 Answer 1

Reset to default 9

You don't need to create a new route for the thing you are trying to achieve. You could add an onClick handler to the MenuItem like this:

  <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>

And then in same ponent add the handler:

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

Remember to import your HandbookDoc.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743684614a215384.html

最新回复(0)