zoukankan      html  css  js  c++  java
  • 2019第10周知识总结

    react 事件绑定 函数写法 文档总结

    https://react.docschina.org/docs/handling-events.html

    1 通过 constroucor绑定

    
    class Toggle extends React.Component {
      constructor(props) {
        super(props);
        this.state = {isToggleOn: true};
    
        // This binding is necessary to make `this` work in the callback
        this.handleClick = this.handleClick.bind(this);
      }
    
      handleClick() {
        this.setState(prevState => ({
          isToggleOn: !prevState.isToggleOn
        }));
      }
    
      render() {
        return (
          <button onClick={this.handleClick}>
            {this.state.isToggleOn ? 'ON' : 'OFF'}
          </button>
        );
      }
    }
    
    
    

    2 使用新属性 属性初始化器

    class LoggingButton extends React.Component {
      // This syntax ensures `this` is bound within handleClick.
      // Warning: this is *experimental* syntax.
      handleClick = () => {
        console.log('this is:', this);
      }
    
      render() {
        return (
          <button onClick={this.handleClick}>
            Click me
          </button>
        );
      }
    }
    

    3 回调函数中使用 箭头函数: 这样调用方式有缺点,就是每次 LoggingButton 渲染的时候都会创建一个不同的回调函数。在大多数情况下,这没有问题。然而如果这个回调函数作为一个属性值传入低阶组件,这些组件可能会进行额外的重新渲染。我们通常建议在构造函数中绑定或使用属性初始化器语法来避免这类性能问题。

    class LoggingButton extends React.Component {
      handleClick() {
        console.log('this is:', this);
      }
    
      render() {
        // This syntax ensures `this` is bound within handleClick
        return (
          <button onClick={(e) => this.handleClick(e)}>
            Click me
          </button>
        );
      }
    }
    
  • 相关阅读:
    Quartz2.0以上版本的单机和集群
    Mysql的Haproxy反向代理和负载均衡
    spring AOP原理解析
    Restful接口调用方法超详细总结
    mysql数据库主从同步
    数据备份的OSS接口
    读取properties配置文件的方法
    算法学习——堆排序(二叉树排序)
    回溯算法的实现
    冒泡排序
  • 原文地址:https://www.cnblogs.com/WhiteHorseIsNotHorse/p/10518440.html
Copyright © 2011-2022 走看看