简体   繁体   English

单击子元素未定义 onclick 事件 reactjs

[英]Clicking on child elements gives undefined for onclick event reactjs

I have a component for restaurants list and I have an onClick event for every list-item.我有一个餐厅列表组件,每个列表项都有一个 onClick 事件。 Everything is working fine except when I click any child element of the list item.一切正常,除非我单击列表项的任何子元素。 It responds with nothing or undefined (I don't know because nothing is showing up in the console.) Here is my component:它不响应或未定义(我不知道,因为控制台中没有显示任何内容。)这是我的组件:

import React from "react";

class ResturantsList extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      reviews: {}
    };

  }
  handleReviews = e => {
    const reviewsList = this.props.resturants.find(
      resturant => resturant.id === e.target.id
    );
    console.log(reviewsList.reviews);
  };

  render() {
    let list = this.props.resturants.map(resturant => {
      return (
        <li
          key={resturant.id}
          id={resturant.id}
          className="resturant"
          onClick={this.handleReviews}
        >
          <div className="resturant__hero">
            <h1 className="resturant__name">{resturant.name}</h1>
            <img src={resturant.icon} alt="" className="resturant__icon" />
          </div>
          <div className="resturant__row">
            <p className="item">Rating</p>
            <p className="description">{resturant.rating || "N/A"}</p>
          </div>

        </li>
      );
    });
    return <div>{list}</div>;
  }
}

export default ResturantsList;

So the problem is I can only click on the padding of li to get accurate result otherwise it throws error.所以问题是我只能点击 li 的填充来获得准确的结果,否则会引发错误。 This behavior is new for me and very unexpected.这种行为对我来说是新的,而且非常出乎意料。

edit- Binding in the constructor is not the problem, I had that line but this is not real issue.编辑-构造函数中的绑定不是问题,我有那条线,但这不是真正的问题。 My event working just fine but only when I click li and not any of its child.我的事件工作得很好,但只有当我点击 li 而不是它的任何孩子时。

Use e.currentTarget使用e.currentTarget

Try using e.currentTarget instead of e.target in your handleReviews() , like so:尝试在handleReviews()使用e.currentTarget而不是e.target ,如下所示:

handleReviews = e => {
    const reviewsList = this.props.resturants.find(
      resturant => resturant.id === e.currentTarget.id // <--- Here
    );
    console.log(reviewsList.reviews);
};

I've solved it by adding for each li's a data-id attribute and binding the handleReviews method to this.我通过为每个 li 添加一个 data-id 属性并将 handleReviews 方法绑定到它来解决它。

<li
      key={resturant.id}
      data-id={resturant.id}
      className="resturant"
      onClick={this.handleReviews}
>

And then in handleReviews I'll get the element with ( e.currentTarget.dataset.id ):然后在 handleReviews 中,我将使用 ( e.currentTarget.dataset.id ) 获取元素:

  handleReviews = e => {
    const reviewsList = this.props.resturants.find(
      resturant => resturant.id === e.currentTarget.dataset.id
    );
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM