Home > Back-end >  I want to click on an image and open the link in a new window in reactjs? [closed]
I want to click on an image and open the link in a new window in reactjs? [closed]

Time:10-07

I am creating my portfolio in reactjs and I made thumbnails that you can click on to go to the website that I have created so they can see my work, but it opens in the same window. I would like to click on the thumbnail(image) and for it to open the link(website) in a new window(tab).

CodePudding user response:

You can do something like this for example -

function openInNewTab(url) {
 window.open(url, '_blank').focus();
}

Then when you add the image, use an onClick attribute to call the above function, which opens a new tab with the url specified.

<img src="www.example.com/picture.png" onclick={()=>openInNewTab('www.example.com')} />

CodePudding user response:

If you mean to open in a new tab you can make use for react router:

<Link to="route" target="_blank" rel="noopener noreferrer" />
If its a function that executes the onclick:

<!-- begin snippet: js hide: false console: true babel: false -->

  • Related