React的事件处理函数

在react中实现事件处理,有如下几种

第一种:在constructor函数中用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>;
      }
    }

第二种:使用箭头函数(render中使用箭头函数)

    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函数中bind 使用class fields语法 render中使用箭头函数 在render中使用bind
render时生成新函数
性能 无影响 无影响 有影响 有影响
可直接携带参数

在render中直接bind或者箭头函数都会影响性能,原因在于,在render 中的bind和箭头函数在每次render时都会创建新的函数,导致子组件的props发生改变,这在PureComponent中会影响性能,除非自己在shouldComponentUpdate中进行优化。

参数传递

直接传递参数,render中使用箭头函数

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

直接传递参数,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>
        )
      }
    }
  
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 作为一个合格的开发者,不要只满足于编写了可以运行的代码。而要了解代码背后的工作原理;不要只满足于自己的程序...
    六个周阅读 8,719评论 1 33
  • 40、React 什么是React?React 是一个用于构建用户界面的框架(采用的是MVC模式):集中处理VIE...
    萌妹撒阅读 1,239评论 0 1
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 8,202评论 2 35
  • 以下内容是我在学习和研究React时,对React的特性、重点和注意事项的提取、精练和总结,可以做为React特性...
    科研者阅读 8,456评论 2 21
  • 每个人都会从文学梦开始的地方 学习文学梦 从小做起从我做起 学习每一点每一滴文学知识 学习好了文学梦开始的地方 就...
    王密亮阅读 183评论 0 0

友情链接更多精彩内容