reactjs es6, .map function not triggering onclick

2020-05-03 09:53发布

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条回答
闹够了就滚
2楼-- · 2020-05-03 10:18

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}>
查看更多
登录 后发表回答