Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint says an anchor needs a valid, navigable href in React

ErrorThe href attribute requires a valid address. Provide a valid, navigable address as the href value.

Short answer

The rule wants every <a> to be a real link. Anchors with href="#", javascript:void(0), an empty href or no href at all are flagged. If the element runs code, replace it with a <button>. If it should navigate, give it a real path. For router links, tell the rule about your Link component.

  • Applies to eslint-plugin-jsx-a11y (recommended config), React 19, ESLint 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run ESLint on the file and read which of the three checks fired: missing href, invalid href, or anchor used as a button. The message text points at the check.

    npx eslint src/components/Menu.jsx
  2. 2Match what you see, then jump to its fix

    1. 1<a href="#" onClick={...}> or <a onClick={...}> with no hrefThe anchor only runs a click handlerFix
    2. 2href="#", href="", href="javascript:void(0)", or href={undefined}/{null}The href is a placeholder or empty valueFix
    3. 3The warning is on <Link to="/x"> after you added it to the rule's components listA router Link component uses a prop other than hrefFix

What does the error look like?

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

Cause 1 of 3

The anchor only runs a click handler

1Check

Search for anchors that have onClick together with a placeholder href or no href. The rule's preferButton check exists for exactly this.

grep -rnE "<a[^>]*(href=\"#\"|javascript:)" src/

2Fix

An element that performs an action is a button. Use <button type="button">. If it must look like a link, style the button with CSS: transparent background, no border, underlined text.

// before
<a href="#" onClick={openMenu}>Menu</a>

// after
<button type="button" className="link-style" onClick={openMenu}>Menu</button>

3Verify

Run ESLint again on the file; the warning for that line is gone.

npx eslint src/components/Menu.jsx

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The href is a placeholder or empty value

1Check

The invalidHref check rejects JavaScript strings, empty values and a bare # even when no onClick is present. A href coming from data can also be undefined or null for some rows.

grep -rnE "href=(\"\"|\{(undefined|null)\})" src/

2Fix

Point the anchor at a real destination such as a path or a fragment id (/pricing or #top). If the destination is not known yet, render a button or leave the element out until it is.

<a href="/pricing">Pricing</a>
<a href="#top">Back to top</a>

3Verify

Lint again, then open the link with the keyboard (Tab, Enter) to confirm it navigates.

npx eslint src/

Confirmed by github.com

Did this fix it?

Cause 3 of 3

A router Link component uses a prop other than href

1Check

The rule checks <a> always. Extra components are only checked if listed under components, and extra link props only if listed under specialLink. If you listed Link without the prop it uses (for example to), it reports a missing href.

grep -n "anchor-is-valid" -A 8 eslint.config.js .eslintrc.json 2>/dev/null

2Fix

List the component and the prop it uses for the address in the rule options.

"jsx-a11y/anchor-is-valid": [
  "error",
  {
    "components": ["Link"],
    "specialLink": ["to"],
    "aspects": ["noHref", "invalidHref", "preferButton"]
  }
]

3Verify

Lint a file with <Link to="/pricing"> and confirm no warning.

npx eslint src/App.jsx

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Decide when writing the element: navigates means <a href>, does something means <button>.
  • Keep the jsx-a11y recommended config on in CI so a placeholder link fails the build.
  • Style buttons that must look like links once, in one CSS class.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.