es6編寫reactjs事件處理函數綁定this三種方式


第一種:官方推薦的:

class LoginControl extends React.Component {
  constructor(props) {
    super(props);
    this.handleLoginClick = this.handleLoginClick.bind(this);
    this.handleLogoutClick = this.handleLogoutClick.bind(this);
    this.state = {isLoggedIn: false};
  }

  

第二種:比較方便

render() {
    return (
      <div onClick={this.handleClick.bind(this)}>ES6方式創建的組件</div>
    );
  }

  

第三種:箭頭函數

return (
      <button onClick={(e) => this.handleClick(e)}>
        Click me
      </button>
    );

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM