在react中删除内联点击侦听器的正确方法是什么?
var ClickComponent = React.createClass({
render: function () {
return (<div onClick={this.onClickStuff} className="orange noselect">
Click me {this.state.clicks || ''}
</div>);
},
getInitialState: function () {
return {
clicks: 0
};
},
onClickStuff: function (e) {
this.setState({
clicks: this.state.clicks + 1
});
if(10 < this.state.clicks){
// remove listener
}
}
});
答案 0 :(得分:0)
在没有onClick
事件的情况下重新渲染您的组件:
onClick={10 < this.state.clicks?undefined:this.onClickStuff}
看起来像:
var ClickComponent = React.createClass({
render: function () {
return (<div onClick={10 < this.state.clicks?undefined:this.onClickStuff} className="orange noselect">
Click me {this.state.clicks || ''}
</div>);
},
getInitialState: function () {
return {
clicks: 0
};
},
onClickStuff: function (e) {
this.setState({
clicks: this.state.clicks + 1
});
}
});