Why did this produce error when I'm doing a click event on a child element here? It also produced an error when I attached onClick={this.handleChangeCity} in the
"cities"` class
I would love to hear how you get around this.
Eslint error: [eslint] Visible, non-interactive elements with click handlers must have at least one keyboard listener. (jsx-a11y/click-events-have-key-events) [eslint] Static HTML elements with event handlers require a role. (jsx-a11y/no-static-element-interactions) (JSX attribute) onClick: any
import React from "react";
import ReactDOM from "react-dom";
import jsonData from "./navigation.json";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
cities : jsonData.cities,
currentCity: jsonData.cities[0].label,
};
this.handleChangeCity = this.handleChangeCity.bind(this);
}
createCities(){
//let cityList = [];
let children = [];
let cityClass = 'oneCity';
let activeCityClass = `${cityClass} active`;
this.state.cities.forEach((city, index) =>{
let divClass = index ? cityClass : activeCityClass;
children.push(<div
onClick={this.handleChangeCity} ===> error when this is added
className={divClass}
data-city={city.label}
key={index}>{city.label}</div>)
});
return children;
}
I'm not sure why this is happening...
handleChangeCity = (event) => {
event.preventDefault();
console.log(event.currentTarget);
this.setState({
currentCity: event.currentTarget.key
});
}
render() {
return (
<section className="container">
<div className="navigation">
<div className = "cities clearfix">
{this.createCities()}
</div>
<div className="underline"></div>
</div>
</section>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));