0

I'm developing a web page with React + Material UI. I want to open a new tab to the link specified and I tried this. It redirects to the link if I do not add target= _blank but it opens in the same tab. If I add target=_blank the button doesn't work at all? The react-route-dom version I have is ^5.2.0. I've tried it on edge and chrome so the problem is not browser specific. I'm using has router and the app might be deployed to with different base url names so I want to avoid hardcoding the url with "http://...".Any help will be appreciated.

 const markLink = (
            <Link
                to={
                    {
                        pathname: "/AssessmentList",
                        search: "?groupId =" + this.state.groupId + " & unitOfferingId=" + this.state.unitOfferingId
                            + "&userId=" + this.state.userId + "&role=" + this.state.role + "&groupName=" + this.state.selectedGroup.group_name
                    } 
               
                } 
                target="_blank"
                style={{ textDecoration: "none", color: "white" }}
            >

                Mark
            </Link>);

Then I use it in my button like this

<ButtonMenu items={[]} name={markLink} working={this.state.project_archived} action={() => {
            }} />

EDIT: I also tried target={"_blank"}. I've tried the suggestions on previous posts but it seems like most pf the URL's need to be "http://" and not the relative path.I'm not sure how to pass in dynamic URLs.

4
  • Did you try target={"_blank"} ? Commented Oct 19, 2020 at 12:00
  • Yes I tried that as well Commented Oct 19, 2020 at 12:00
  • Does this answer your question? React-Router open Link in new tab Commented Oct 19, 2020 at 12:04
  • No i've visited that page.Most of the answers there must input URL using "http:" and I'm not sure how to pass in a dyanamic url? Commented Oct 19, 2020 at 12:07

1 Answer 1

0

The point of using react-router is to render components depending on the url. If you are planning to open the page on a new link just use a regular a element.

const { groupId, unitOffering, userId, role, selectedGroup } = this.state;

const path = `/AssessmentList?groupId=${groupId}&unitOfferingId=${unitOfferingId}&userId=${userId}&role=${role}&groupName=${selectedGroup.group_name}`;

return (
  <a
    href={path}
    target="_blank"
    rel="noopener noreferrer"
    style={{ textDecoration: "none", color: "white" }}>
    Mark
  </a>
);
Sign up to request clarification or add additional context in comments.

4 Comments

If I don't use the <Link> tag, the route will be without the # at the beginning and this will cause the page to be not found and re routed to the index page.I'm using hash router.
Can you not add the # to the path variable? But my previous answer still stands, if you are opening in a new tab, there is no need to use Link from react-router.
However, I'm trying to style it like a button and if I embed a button tag inside the <a> tag, the problem still persists?
That's a different question :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.