简体   繁体   中英

Open New Tab When User Clicks on Link to External Website

I'm developing a recommendations application written in React, and want the user to be able to be sent to external websites when the component containing the website's information has been clicked.

Currently, I have a wrapper that, when clicked, pushes the new URL to the history object from the React library. The source code is shown below.

<VStack onClick={() => history.push(<url here>)}> 
     <inside of component>
</VStack>

Unfortunately, the URL is being loaded in the same window, but I want the user to remain on the website, in the original tab.

Update your onClick function to

 onClick={() => window.open(urlHere)}

see more of window.open here

You can perhaps try setting the target attribute to "_blank" in the Link tag itself.

example:

<Link to="/somewhere" target="_blank">anything</Link>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM