美文网首页框架学习
React 事件处理机制

React 事件处理机制

作者: beijing_beijing | 来源:发表于2017-04-04 21:19 被阅读466次

    React在处理事件和HTML中JS处理事件不同,本文介绍React中的事件处理机制。React中不同通过返回false来阻止事件的默认行为,必须明确使用preventDefault.

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

    用ES6 class构建组件的时候,处理事件,需要注意的是,利用bind来绑定callback反馈。因为我们没有使用handleClick()来绑定到onClick上,仅仅使用{this.handleClick}.所以需要在constructor中来绑定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>
        );
      }
    }
    

    当然我们还可以用另外两种方法来绕过此绑定行为,但是依旧推荐在constructer绑定。

    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 事件处理机制

        本文链接:https://www.haomeiwen.com/subject/yhmzottx.html