zoukankan      html  css  js  c++  java
  • React事件处理程序

    function ActionLink() {
      function handleClick(e) {
        e.preventDefault();
        console.log('The link was clicked.');
      }
    
      return (
        <a href="#" onClick={handleClick}>
          Click me
        </a>
      );
    }

    When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered.

    Becareful about 'this':

    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>
        );
      }
    }
    
    ReactDOM.render(
      <Toggle />,
      document.getElementById('root')
    );

    或者可以用下面的方法解决: (利用ES6箭头函数)

    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>
        );
      }
    }

    或者

    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>
        );
      }
    }

     以上均来自于react官网docs。

    可以在js 中用debugger来断点调试

  • 相关阅读:
    Hibernate批量处理数据、HQL连接查询
    Hibernate二级缓存配置
    Hibernate一对一关联映射配置
    Hibernate延迟加载
    Hibernate双向多对多关联
    映射对象标识符
    06章 映射一对多双向关联关系、以及cascade、inverse属性
    解析ThreadLocal
    save()、saveOrUpdate()、merge()的区别
    第一个Shell脚本
  • 原文地址:https://www.cnblogs.com/ariel-zhang/p/6812618.html
Copyright © 2011-2022 走看看