create-react-app: Lint rules don't allow links with javascript:void href, or any alternative.
For a variety of valid reasons, you may want to have html anchors with a href that is null for a time. One way of doing this is href='javascript:void(0)'
, however this triggers:
Script URL is a form of eval no-script-url" lint error.
Another way is href='#'
. This triggers:
Links must not point to "#". Use a more descriptive href or use a button instead jsx-a11y/href-no-hash
Another way is <a href>
, This triggers:
Warning: Received 'true' for a non-boolean attribute 'href'.
Are there ways to achieve the desired effect without lint errors, otherwise can one of these be removed?
See: #808
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 10
- Comments: 21 (9 by maintainers)
href={null}
should work.bump š please get rid of this rule
To get what @peterbraden asked, I just write ā#/ā as href value:
<a href="#/">...</a>
The warning is gone, and the output HTML is valid.
My apologies, maybe we should consider loosening this ruleā¦
Maybe some clarification is in order as to why this rule exists and why it is a good idea to follow it instead of finding ways to go around it.
I currently have a PR open that will make the error message clearer and also show where to find more information. The current docs for that can be found here https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/anchor-is-valid.md. It contains an explanation of why this rule exists.
But to expand on it, abusing an
anchor
tag for anything but navigation goes way beyond being contrary to the specification of this element. It causes a very real detrimental experience to many users out there when confronted with this situation.Here I am referring to any user that uses a keyboard to navigate the internet. That includes users who rely on screen reader software to use the web. Exact statistics are hard to find due to the privacy impact it would have, but one study shows that the number of users likely to use a keyboard alone just due to a disability being about 7% of the working adults in the UK, US and Canada. https://www.powermapper.com/blog/website-accessibility-disability-statistics/
This does not even include those who choose to use a keyboard or sustained a temporary disability like a broken arm etc.
The point is we are talking about millions and millions of people.
If an
anchor
has nohref
it does not act a link. It becomes, what the spec calls, a placeholder. In fact you are unable to select and activate it using a keyboard alone. Regardless of whether it contains an onClick handler or not. If this element is important to the function of the application it means that the application would block around 7% or more of the population from using this application at all.If the
anchor
container a dummyhref
you will be able to interact with it using the keyboard, however the interaction will be unexpected. Screen reader users have the elements announced to them, so, if you come across a link it says āLinkā, regardless of how it is styled. And links are expected to navigate. It is extremely confusing to a screen reader user when they encounter a link, activate it and no navigation happens.By using the fix @gaearon suggests above and turning it into a button, EVERYONE can interact with it. All keyboard users can interact with it while screen reader users hear āButtonā and expect it to perform an action and not a navigation. All that, without having to hack the
href
or suppress the rule.The rule is not there to be an irritation but to try and assist us in making applications that can be used by the widest group of users possible š
I use href='# ā (notice the space after #) which retains the css of a tag and doesnāt alter the URL in any other way a normal # would do.
Just for visually design purposes when the route to that specific is either not defined or not implemented. Iād rather use the href attribute than add a class to use cursor: pointer
I think instead of disabling this line we need to properly explain how to style buttons like links. Just like Bootstrap does.
For example:
Then use a button:
AFAIK that would correctly behave in an accessible way without hacks.
Itās still incorrect use of an anchor tag. Why not use button?
This doesnāt work as it means that the href isnāt passed to the dom, meaning that the style of the anchor doesnāt get the link attributes.
Iām aware of the rationale. In fact the desire to keep semantic navigation elements as anchors rather than use a button was the entire reason I didnāt just add an onClick to a different element.
Would be really nice to remove that rule. Currently, in react-scripts 1.x it is not enforced, so having a big codebase it is hard to try out 2.0 version, without significate changes in your code.