Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle click on child element

Tags:

reactjs

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"));
like image 239
guest Avatar asked Feb 09 '19 21:02

guest


People also ask

How do I prevent a parent's onClick event from firing when a child anchor is clicked?

By a adding an onClick function for the child modal (content div) mouse click events are prevented to reach the 'closeLogin' function of the parent element. This did the trick for me and I was able to create a modal effect with 2 simple divs.

How do you only trigger parent click event when a child is clicked?

To only trigger parent click event when a child is clicked with JavaScript, we call stopPropagation . parent. addEventListener( "click", (e) => { e. stopPropagation(); console.

How do I stop a click event in HTML?

To stop the click event from propagating to the <div> element, you have to call the stopPropagation() method in the event handler of the button: btn. addEventListener('click', (e) => { e. stopPropagation(); alert('The button was clicked!


Video Answer


2 Answers

Well it looks like you got 2 linting errors. First is Visible, non-interactive elements with click handlers must have at least one keyboard listener.. That looks like it's about accessibility, so people can use keyboard and mouse to interact. So you have to add a key handler too, check out this similar post.

The second issue is Static HTML elements with event handlers require a role.. This is a linting option which prevents binding of event handler to generic things like a <div/>. Check out this post for an answer to that.

Alternatively, you could change or disable your linting so these are no longer issues.

like image 116
Jayce444 Avatar answered Oct 19 '22 12:10

Jayce444


These are linting issues as mentioned in ESLint documentation as following:

Enforce onClick is accompanied by at least one of the following: onKeyUp, onKeyDown, onKeyPress.

To fix this you can use any of the above mentioned props and change your code to this:

<div 
   onClick={this.handleChangeCity}
   onKeyDown={this.handleChangeCity}
   className={divClass} 
   data-city={city.label}
   key={index}>{city.label}
</div>
like image 28
Burhan Maseel Avatar answered Oct 19 '22 12:10

Burhan Maseel