Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React site warning: The href attribute requires a valid address. Provide a valid, navigable address as the href value jsx-a11y/anchor-is-valid

I am getting a warning on a React site I built

./src/components/layout/Navbar.js [1]   Line 31:  The href attribute requires a valid 
address. Provide a valid, navigable address as the href value  jsx-a11y/anchor-is-valid

on the following code:

          <p>
            {isEmpty(profile.website) ? null : (
              <a
                className="text-white p-2"
                href={profile.website}
                target="#"
              >
                <i className="fas fa-globe fa-2x" />
              </a>
            )}
            {isEmpty(profile.social && profile.social.twitter) ? null : (
              <a
                className="text-white p-2"
                href={profile.social.twitter}
                target="#"
              >
                <i className="fab fa-twitter fa-2x" />
              </a>
            )}
            {isEmpty(profile.social && profile.social.facebook) ? null : (
              <a
                className="text-white p-2"
                href={profile.social.facebook}
                target="#"
              >
                <i className="fab fa-facebook fa-2x" />
              </a>
            )}
          </p>

Even though the warning appears only for the first link, the same warning occurs on the next link if I remove the first link temporarily or change the href of the first link to a static URL.

The links need to appear as just an icon.

I have tried things such as using a button (did not have the correct look), using a function to open the dynamic url, and trying to force the href to be a string by using '' + {profile.website}. Many other suggestions have not worked.

Is there a way to prevent the error, without changing the jsx-a11y rules? Is what I have done not a good pattern, or is it just a bug in React or JSX?

like image 947
msm1089 Avatar asked Oct 14 '18 08:10

msm1089


4 Answers

Use href="/#" to replace href="#" OR href="javascript:;" OR href="javascript:void(0);"

It should remove the warnings.

like image 198
Tunji Oyeniran Avatar answered Oct 16 '22 21:10

Tunji Oyeniran


These worked for me to get rid off the warning;

<a href="#/">...</a>  
<a href={() => false}>...</a>
like image 28
Jonathan Akwetey Okine Avatar answered Oct 16 '22 22:10

Jonathan Akwetey Okine


I've used href="!#" to remove warnings.

like image 21
Bojan Vlatkovic Avatar answered Oct 16 '22 22:10

Bojan Vlatkovic


To also prevent default I use this:

<a href="/#" onClick={(e) => e.preventDefault()}>Link Text</a>
like image 15
Hovhannes Bantikyan Avatar answered Oct 16 '22 21:10

Hovhannes Bantikyan