reactjs es6, .map function not triggering onclick

自作多情 提交于 2019-12-20 07:29:19

问题


Reactjs, ES6 issue: Very simple onclick event in a .map function not working. When I inspect it, this in handleclick function represent _this5 and this in .map bind represents _this6.

class LineItem extends React.Component{
  constructor(props){
    super(props);
  }
  handleClick=(event)=> {
    console.log(this);
  }
  render(){
    return(
      <div>
        { 
          this.props.Lines.map((line,i)=> {
                  return <div className="subcontent">
                          <div className="row-wrapper plan-content">
                            <Row className="show-grid" onClick={this.handleClick()}>
                              <span>{line.lineName}</span>
                            </Row>
                          </div>
                          <LineStatus LineInfo={line} Camp={this.props.Camp} key={i}/>
                        </div>;
                }, this)
              }
    </div>

回答1:


Right now you are calling the function and using the return value as the onClick. You just want to pass the function reference like this:

<Row className="show-grid" onClick={this.handleClick}>


来源:https://stackoverflow.com/questions/37737467/reactjs-es6-map-function-not-triggering-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!