问题
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