How to fix 'Static HTML elements with event handlers require a role.'?
Asked Answered
A

7

52

My reactjs styledcomponent contains this code:

<a styling="link" onClick={() => this.gotoLink()}>
  <SomeComponent /> 
</a>

This works fine but the eslint is complaining:

Static HTML elements with event handlers require a role.

How can I fix this error?

Acting answered 20/1, 2019 at 7:39 Comment(1)
Can you try adding the role=button to the anchor tagExcavation
W
53

you need to add a role props in your a tag to avoid this warning, for example a button

<a role = "button" styling="link" onClick={() => this.gotoLink()}>
  <SomeComponent /> 
</a>

I guess it is because the HREF props is missing in your anchor tag (not sure)

Wimer answered 20/1, 2019 at 7:46 Comment(2)
You may also need to add tabIndex='0' to avoid "'button' interactive role must be focusable"Kelp
for me somehow tabIndex='0' was not working and tabIndex={0} was workingPoplin
E
25

In my case, I used aria-hidden="true", then I was able to commit.

Before:

<i className="pwdicon" onClick={togglePasswordVisiblity} >

After I updated with aria-hidden:

<i className="pwdicon" onClick={togglePasswordVisiblity} aria-hidden="true" >

My problem was resolved.

Reference Link : https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/click-events-have-key-events.md

Etymologize answered 23/9, 2020 at 7:18 Comment(0)
A
8

The earlier answers do give specific examples, what I was missing is a list of roles.
If someone is looking for other roles, a partial list is listed here.
An example of a missing role is tab which I needed.


Edit: per the comment request, my personal error was solved by adding role, but I was missing a list of allowed roles, see above for a partial list and a more complete (or possibly complete) list here, thanks to the comment by Caiof.

Ampulla answered 28/1, 2021 at 14:9 Comment(2)
Please edit to include exact code to solve the OP's problem, in addition to the useful info you have added. As is, this is not a stand alone answer, but a Comment to another post. Indeed, some answers provided do not even use the role property, so without context, it may not even universally apply. For reference, the help section, linked at the top of every page, does indicate that exact coded necessary to solve the OP's issue must be embedded in Answers, whenever possible. Alternatively, post as a Comment to one or more posts. Links like this can be valuable as Comments.Erny
A more complete list of roles: w3.org/TR/wai-aria-1.1/#role_definitionsPricilla
B
3

You need to set the role explicitly. So, try the next code:

<a role="button" styling="link" onClick={this.gotoLink}>
  <SomeComponent /> 
</a>

Also, as you can see I've modified the onClick handler by replacing arrow function on regular declaration. It would reduce annoying and expensive calculations.

Byram answered 20/1, 2019 at 8:7 Comment(0)
P
3

just add aria-hidden to it

<a aria-hidden styling="link" onClick={() => this.gotoLink()}>
  <SomeComponent /> 
</a>
Politick answered 16/1, 2022 at 13:28 Comment(0)
G
1

Just need to add the 'aria-hidden' attribute, like this:

<div onClick={handleClickCollectionCard} aria-hidden="true">
Gifford answered 22/7, 2022 at 15:27 Comment(0)
S
0

You can simply add 'aria-hidden' attribute to the element. For example, I was working with <span> tag so I did the following:

<span aria-hidden="true" onClick={() => showDialog()}>Click here</span>

And, it worked fine!

Sarnen answered 10/10, 2023 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.