ESLint says an anchor needs a valid, navigable href in React
The 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.
Fix it fast
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.jsx2Match what you see, then jump to its fix
- 1
<a href="#" onClick={...}> or <a onClick={...}> with no hrefThe anchor only runs a click handlerFix - 2
href="#", href="", href="javascript:void(0)", or href={undefined}/{null}The href is a placeholder or empty valueFix - 3
The 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
- 1
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.jsxConfirmed by developer.mozilla.org
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
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/null2Fix
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.jsxConfirmed by github.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix