光速 React

技術 科技優家 2017-05-19

/* 給 prop 傳入一個行內綁定的函數(包括 ES6 箭頭函數)實質上是在每次父組件 render 時傳入一個新的函數。 */ render { return ( <div> <a onClick={ => this.doSomething }>Bad</a> <a onClick={ this.doSomething.bind( this ) }>Bad</a> </div> ); } /* 應該在構造函數中處理函數綁定並且將已經綁定好的函數作為 prop 的值 */ constructor( props ) { this.doSomething = this.doSomething.bind( this ); //or this.doSomething = (...args) => this.doSomething(...args); } render { return ( <div> <a onClick={ this.doSomething }>Good</a> </div> ); }