2016-06-20 32 views
0

Was ist der richtige Weg, um Inline-Klick-Listener in reagieren zu entfernen?Inline-Listener in Reaktion entfernen

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 
     } 
    } 
    }); 

Antwort

0

Re-machen Ihre Komponente ohne onClick Ereignis:

onClick={10 < this.state.clicks?undefined:this.onClickStuff}

Es sieht aus wie:

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 
      }); 
     } 
    });