我最初在这个组件中使用硬编码的
我添加了
我做错了什么吗?
<th>
,但后来意识到将它们放入数组并在渲染之前调用可以节省时间。但现在,onClick事件不会触发。我添加了
<th id="category" onClick={this.handleClick}>test cat</th>
以显示它仍然有效。似乎因为我正在构建的数组位于return()之外,它不会触发onClick事件。我做错了什么吗?
var Table = React.createClass({
handleClick: function(sortid) {
alert(sortid.target.id);
this.props.sortClick(sortid.target.id);
},
render: function() {
...
var columnDiv = this.props.columns.map(function(column, i) {
return (
<th id={column} key={i} onClick={this.handleClick}>
{column}
</th>
);
});
return (
<table className="table table-hover">
<thead>
<tr>
<th id="category" onClick={this.handleClick}>test cat</th>
{columnDiv}
</tr>
</thead>
<tbody>
{rows}
</tbody>
</table>
);
}
});
}.bind(this));
- Brady.bind(this)
大致意思是:将我现在拥有的this
,使其在函数中,当我说this
时,它是相同的东西。这很重要,因为在函数内部,this
通常指的是函数本身。 - Mark Bolusmjakthis
,因此它会默认为全局对象,或在严格模式下为undefined。您可以使用以下代码进行测试:[1].map(function(){return this})
- Brigand