Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 877
bier hier Avatar asked Jan 20 '19 07:01

bier hier


5 Answers

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)

like image 124
Jerome Avatar answered Nov 19 '22 01:11

Jerome


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

like image 38
Kodali444 Avatar answered Nov 19 '22 01:11

Kodali444


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.

like image 7
MikeL Avatar answered Nov 18 '22 23:11

MikeL


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.

like image 3
Sviat Kuzhelev Avatar answered Nov 18 '22 23:11

Sviat Kuzhelev


just add aria-hidden to it

<a aria-hidden styling="link" onClick={() => this.gotoLink()}>
  <SomeComponent /> 
</a>
like image 2
navid sotoudeh mehr Avatar answered Nov 18 '22 23:11

navid sotoudeh mehr