0

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
  • FYI, the issue has nothing to do with React. You would have the same problem if you used any other event binding API. Commented Jun 10, 2016 at 15:07

1 Answer 1

2

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}>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.