小编典典

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

reactjs

Reactjs,ES6问题:.map函数中非常简单的onclick事件不起作用。当我检查它时,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>

阅读 413

收藏
2020-07-22

共1个答案

小编典典

现在,您正在调用该函数,并将返回值用作onClick。您只想像这样传递函数引用:

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