anchorValidity
Reports invalid usage of anchor elements.
✅ This rule is included in the jsxlogicalandlogicalStrictpresets.
The HTML <a> (“anchor”) element represents a link to some place on the current page or another page.
If an <a> does not have a valid href attribute than it is not a valid anchor.
Invalid anchors are most commonly seen when developers use an <a> with an onClick, rather than the more semantic <button>.
Anchor elements should only be used for navigation, not triggering modifications to the current page.
Examples
Section titled “Examples”<JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="#" /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="javascript:void(0)" /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a DOMAttributes<HTMLAnchorElement>.onClick?: MouseEventHandler<HTMLAnchorElement> | undefined
onClick={() => {}} /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="#" DOMAttributes<HTMLAnchorElement>.onClick?: MouseEventHandler<HTMLAnchorElement> | undefined
onClick={() => {}} /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="https://example.com" /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="/path" /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="#section" /><JSX.IntrinsicElements.button: DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>
button DOMAttributes<HTMLButtonElement>.onClick?: MouseEventHandler<HTMLButtonElement> | undefined
onClick={() => {}} /><JSX.IntrinsicElements.a: DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>
a AnchorHTMLAttributes<HTMLAnchorElement>.href?: string | undefined
href="https://example.com" DOMAttributes<HTMLAnchorElement>.onClick?: MouseEventHandler<HTMLAnchorElement> | undefined
onClick={() => {}} />When Not To Use It
Section titled “When Not To Use It”If you use a framework that automatically populates anchors with proper href attributes, then you can disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
jsx-a11y/anchor-is-valid - Oxlint:
jsx_a11y/anchor-is-valid
Made with ❤️🔥 around the world by
the Flint team and contributors.