Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a link in a new Tab in NextJS?

Tags:

next.js

How can i open a link in a new Tab in NextJS ? i tried this :

      <Link href="https://twitter.com/" passHref>
        <a target="_blank">
          <div className={`${dark ? styles.iconTwitterWhite : styles.iconTwitter} mr-3`} />
        </a>
      </Link>

It opens the link in a new Tab but i have an ESLint message saying :

ESLint: The href attribute is required for an anchor to be keyboard accessible. Provide a valid, navigable address as the href value. If you cannot provide an href, but still need the element to resemble a link, use a button and change it with appropriate styles.

Is there another way to do it ?

like image 445
Abdo Rabah Avatar asked Jan 08 '21 16:01

Abdo Rabah


People also ask

How do I add a external link in NextJs?

To add an external link to the Next. js Link component we should use attribute passHref. This attribute is set to false by default. This attribute forces Link to send the href property to its child.

How do I make a link open in a new tab in HTML?

You can make a HTML link open in a new tab by adding the target=”_blank” attribute. You should insert this after the link address.

How do I open a new tab programmatically?

You can, in Firefox it works, add the attribute target="_newtab" to the anchor to force the opening of a new tab. window. open('page. html','_newtab');


4 Answers

As this is an external link, you don't need to use Link

<a target="_blank" href="https://twitter.com/" rel="noopener noreferrer">     <div className={`${dark ? styles.iconTwitterWhite : styles.iconTwitter} mr-3`} />  </a>        
like image 78
enoch Avatar answered Oct 12 '22 05:10

enoch


I've heard there are cases where you want to stick with Link (i18n stuff that Link helps with) so you can use passHref (which just passes the href to the immediate child) like so:

<Link href="/" passHref>   <a target="_blank" rel="noopener noreferrer">     Foo   </a> </Link> 
like image 38
corysimmons Avatar answered Oct 12 '22 05:10

corysimmons


The path in my project: public/NameFile/NamePdf.pdf

Note: 1) Don't forget the dependence for "Link" => import Link from 'next/link' 2) As you can see, I did not put "public" in the link. >> Because you simply don't need to put it because NextJs is smart 🧠.

      <Link href="./NameFile/NamePdf.pdf" download>
          <a target="_blank">
              {your code ......}
          </a>
      </Link>
like image 42
Your code Avatar answered Oct 12 '22 05:10

Your code


Remember that the NextJs link use to move other page of current web without reload right?

So in this case you want to go external link which not exist in you web so you don't need to use Link provided by NextJs. You can use any other link.

For Example:

  1. Vanilla HTML anchor tag
  2. React-Bootstrap Link
  3. Chakra-UI Link

and there are so many libraries out there.

like image 40
Ahmed Shaikh Avatar answered Oct 12 '22 07:10

Ahmed Shaikh