简体   繁体   English

reactjs es6,.map函数不会触发onclick

[英]reactjs es6, .map function not triggering onclick

Reactjs, ES6 issue: Very simple onclick event in a .map function not working. Reactjs,ES6问题:.map函数中非常简单的onclick事件不起作用。 When I inspect it, this in handleclick function represent _this5 and this in .map bind represents _this6. 当我检查它时,handleclick函数中的这个表示_this5,而.map绑定中的这个表示_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>

Right now you are calling the function and using the return value as the onClick . 现在,您正在调用该函数,并将返回值用作onClick You just want to pass the function reference like this: 您只想像这样传递函数引用:

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

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

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