🎁 The Js-Craft Guide to React is now available with a 30% off discount!

Open a link in a new tab in React

Let's explore 2 ways of how to open a link in a new tab in React.

Opening a link in a new tab in React using the target attribute

The first obvious step is to use the target="_blank" attribute.

const newTabLink = ({url, text})=> {
    return(
        <a href={url} rel="noopener" target="_blank">
            {text}
        </a>
    )
}

Keep in mind to use rel="noopener" to avoid the security vulnerability that target=”_blank” may expose.

Opening a new tab at onClick() in React

The second option is to open a url in a new tab when the user clicks on a button or other React component.

We can do this using the window.open() method.

<button onClick={
        () => window.open('https://www.js-craft.io/', '_blank', 'noopener')
    }>
    Go to JS Craft
</button>

Keep in mind that also in this case we will need to add the noopener parameter to avoid any phishing vulnerabilities.

Speaking of this we have seen in a former article also how to open a NextJs link in a new tab.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.

10 Javascript AI projects with Langchain & React

Not sure what to build? Dive in with these Javascript AI projects! Learn how to build LLM powered apps using Langchain.js and React! Connect your apps to LLMs such as OpenAI, create agents, use vector databases, or setup AI context.


Leave a Reply

Your email address will not be published. Required fields are marked *

πŸ“˜ The Guide to React Home Screencasts Best of Newsletter Search X

πŸ“˜ - 10 Javascript AI projects with React and LangChain

Hi friend! Before you go, just wanted to let you know about the 10 Javascript AI projects ideas with React and LangChain FREE ebook.

One of the best ways to learn is by doing the work. Choose from these 10 project ideas and start working on topics such as:

  • How does LangChain work
  • Connecting to OpenAI LLM
  • Create AI Agents
  • Simple and Sequential Chains
  • Adding Memory (Chat Context)
  • Prompt Templates
  • Using OutputParsers and Tools for Agents
  • Work with Documents and more...

Keep building and see the new capabilities LLM models can add to your React app! Get all projects as an ebook right to your inbox!

X