reactjs es6,.map 函数不触发 onclick
reactjs es6, .map function not triggering onclick
Reactjs、ES6 问题:.map 函数中非常简单的 onclick 事件不起作用。当我检查它时,handleclick 函数中的 this 代表 _this5,.map 绑定中的 this 代表 _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>
现在您正在调用函数并使用 return 值作为 onClick
。您只想像这样传递函数引用:
<Row className="show-grid" onClick={this.handleClick}>
Reactjs、ES6 问题:.map 函数中非常简单的 onclick 事件不起作用。当我检查它时,handleclick 函数中的 this 代表 _this5,.map 绑定中的 this 代表 _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>
现在您正在调用函数并使用 return 值作为 onClick
。您只想像这样传递函数引用:
<Row className="show-grid" onClick={this.handleClick}>