React的事件處理函數(shù)

在react中實現(xiàn)事件處理,有如下幾種

第一種:在constructor函數(shù)中用bind方法

    class ReactEvent extends Component {
      constructor(props) {
        super(props);
        this.handleClick = this.handleClick.bind(this);
      }
    
      handleClick() {
        console.log('Click');
      }
    
      render() {
        return <button onClick={this.handleClick}>Click Me</button>;
      }
    }

第二種:使用箭頭函數(shù)(render中使用箭頭函數(shù))

    class ReactEvent extends Component {
      handleClick() {
        console.log('Click');
      }
      
      render() {
        return <button onClick={() => this.handleClick()}>Click Me</button>;
      }
    }

第三種:使用class fields語法

     class ReactEvent extends Component {
       handleClick = () => {
         console.log('Click');
       }
     
       render() {
         return <button onClick={this.handleClick}>Click Me</button>;
       }
     }

第四種:在render中使用bind方法

     class ReactEvent extends Component {
       handleClick() {
         console.log('Click');
       }
     
       render() {
         return <button onClick={this.handleClick.bind(this)}>Click Me</button>;
       }
     }

幾種方式的比較

影響 constructor函數(shù)中bind 使用class fields語法 render中使用箭頭函數(shù) 在render中使用bind
render時生成新函數(shù)
性能 無影響 無影響 有影響 有影響
可直接攜帶參數(shù)

在render中直接bind或者箭頭函數(shù)都會影響性能,原因在于,在render 中的bind和箭頭函數(shù)在每次render時都會創(chuàng)建新的函數(shù),導(dǎo)致子組件的props發(fā)生改變,這在PureComponent中會影響性能,除非自己在shouldComponentUpdate中進行優(yōu)化。

參數(shù)傳遞

直接傳遞參數(shù),render中使用箭頭函數(shù)

      this.state.list.map(item => (
          <Button onClick={() => this.handleClick(item.id)}/>
      ))

直接傳遞參數(shù),render中使用bind

      this.state.list.map(item => (
          <Button onClick={this.handleClick.bind(this, item.id)}/>
      ))

推薦的方式

    //子組件
    class Button extends React.PureComponent {
      handleClick = () => {
        this.props.handleClick(this.props.item);
      }
    
      render() {
        return (
          <button onClick={this.handleClick}>hello world</button>
        )
      }
    }
    
    
    //父組件
    class ButtonList extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          list: [1, 2, 3, 4]
        };
      }
      
      handleClick = (item) => {
        console.log('Click', item);
      }
      
      render() {
        const { list=[] } = this.state;
    
        return (
          <div>
            {
              list.map(item => <Button handleClick={this.handleClick} item={item}/>)
            }
          </div>
        )
      }
    }
  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 作為一個合格的開發(fā)者,不要只滿足于編寫了可以運行的代碼。而要了解代碼背后的工作原理;不要只滿足于自己的程序...
    六個周閱讀 8,675評論 1 33
  • 40、React 什么是React?React 是一個用于構(gòu)建用戶界面的框架(采用的是MVC模式):集中處理VIE...
    萌妹撒閱讀 1,185評論 0 1
  • 深入JSX date:20170412筆記原文其實JSX是React.createElement(componen...
    gaoer1938閱讀 8,183評論 2 35
  • 以下內(nèi)容是我在學(xué)習(xí)和研究React時,對React的特性、重點和注意事項的提取、精練和總結(jié),可以做為React特性...
    科研者閱讀 8,408評論 2 21
  • 每個人都會從文學(xué)夢開始的地方 學(xué)習(xí)文學(xué)夢 從小做起從我做起 學(xué)習(xí)每一點每一滴文學(xué)知識 學(xué)習(xí)好了文學(xué)夢開始的地方 就...
    王密亮閱讀 176評論 0 0

友情鏈接更多精彩內(nèi)容