浅析React之事件系统(二)

作者: aaronisme | 来源:发表于2017-02-05 10:57 被阅读639次

上篇文章中,我们谈到了React事件系统的实现方式,和在React中使用原生事件的方法,那么这篇文章我们来继续分析下,看看React中合成事件和原生事件混用的各种情况。

上一个例子

在上篇文章中,我们举了个例子。为了防止大家不记得,我们来看看那个例子的代码。

class App extends React.Component {

  constructor(props){
    super(props);
    
    this.state = {
      show: false
    }
    
    this.handleClick = this.handleClick.bind(this)
    this.handleClickImage = this.handleClickImage.bind(this);
  }
  
  handleClick(){
   this.setState({
     show: true
   })
  }
  
  componentDidMount(){
    document.body.addEventListener('click', e=> {
      this.setState({
        show: false
      })
    })
  }
  
  componentWillUnmount(){
    document.body.removeEventListener('click');
  }
  
  handleClickImage(e){
    console.log('in this ')
    e.stopPropagation();
  }
  
  render(){
    return (
      <div className="container">
        <button onClick={this.handleClick}>Open Image</button>
          <div className="img-container" style={{ display: this.state.show ? 'block': 'none'}} onClick={this.handleClickImage}>
            ![](https://img.haomeiwen.com/i65230/3ad92c3b2a23e6b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
          </div>
      </div>
    )
  }
}
ReactDOM.render(<App />, document.getElementById('root'));

这有什么问题呢? 问题就在于,如果我们点击image的内部依旧可以收起Image,那么这是为什么呢?这是因为我们及时点击了Image的内部,body上绑定的事件处理器依旧会执行,这样就让我们的image收起来了。那我们如果不想让image收起来改怎么做呢?

首先的想法是停止冒泡,如果我们在img-container中就停止冒泡了是不是就可以让image不消失了呢?比如这样:

...
handleClickImage(e){
    e.preventDefault();
    e.stopPropagation();
  }
  
  render(){
    return (
      <div className="container">
        <button onClick={this.handleClick}>Open Image</button>
          <div className="img-container" style={{ display: this.state.show ? 'block': 'none'}} onClick={this.handleClickImage}>
            ![](https://img.haomeiwen.com/i65230/3ad92c3b2a23e6b5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
          </div>
      </div>
    )
  }
...

Open In CodePen

在这里我们定义一个handleClickImage的方法,在其中我们执行取消默认行为和停止冒泡。那是似乎效果并不是我们想要的。因为阻止React事件冒泡的行为只能用于React合成事件中,没法阻止原生事件的冒泡。同样用React.NativeEvent.stopPropagation()也是无法阻止冒泡的。

如何解决这样的问题呢?首先,尽量的避免混用合成事件和原生事件。需要注意的点是:

  1. 阻止react 合成事件冒泡并不会阻止原生时间的冒泡,从上边的例子我们已经看到了,及时使用stopPropagation也是无法阻止原生时间的冒泡的。
  2. 第二点需要注意的是,取消原生时间的冒泡会同时取消React Event。并且原生事件的冒泡在react event的触发和冒泡之前。同时React Event的创建和冒泡是在原生事件冒泡到最顶层的component之后的。我们来看这个例子:
class App extends React.Component {
  
  render(){
   return <GrandPa />;
  }
}

class GrandPa extends React.Component {
  constructor(props){
      super(props);
      this.state = {clickTime: 0};
      this.handleClick = this.handleClick.bind(this);
  }
  
 handleClick(){
   console.log('React Event grandpa is fired');
  this.setState({clickTime: new Date().getTime()})
};
  
  componentDidMount(){
    document.getElementById('grandpa').addEventListener('click',function(e){
      console.log('native Event GrandPa is fired');
    })
  }
  
  render(){
    return (
      <div id='grandpa' onClick={this.handleClick}>
        <p>GrandPa Clicked at: {this.state.clickTime}</p>
        <Dad />
      </div>
    )
  }
}

class Dad extends React.Component {
  constructor(props){
    super(props);
    this.state = {clickTime:0};
    this.handleClick=this.handleClick.bind(this);
  }
  
  componentDidMount(){
    document.getElementById('dad').addEventListener('click',function(e){
      console.log('native Event Dad is fired');
      e.stopPropagation();
    })
  }
  
  handleClick(){
    console.log('React Event Dad is fired')
    this.setState({clickTime: new Date().getTime()})
  }
  
  render(){
    return (
      <div id='dad' onClick={this.handleClick}>
       <p>Dad Clicked at: {this.state.clickTime}</p>
        <Son />
      </div>
     )
  }
}

class Son extends React.Component {
  constructor(props){
    super(props);
    this.state = {clickTime:0};
    this.handleClick=this.handleClick.bind(this);
  }
  
  handleClick(){
    console.log('React Event Son is fired');
    this.setState({clickTime: new Date().getTime()})
  }
  
  componentDidMount(){
    document.getElementById('son').addEventListener('click',function(e){
      console.log('native Event son is fired');
    })
  }
  
  render(){
    return (
      <div id="son">
       <p onClick={this.handleClick}>Son Clicked at: {this.state.clickTime} </p>
      </div>
     )
  }
}

ReactDOM.render(<App />, document.getElementById('root'));

Open in CodePen

在这个例子中我们有三个component,Son Dad,Grandpa。同时定义了React Event handler 和 native event handler,并在Dad的native Event handler中stopPropagation,当我们点击Son or Dad component的时候会发现,React Event handler并没有被trigger。
console里的output为:

"native Event son is fired"
"native Event Dad is fired"

这就说明native Event的停止冒泡可以阻断所有的React Event。所以即使我们是在Dad上停止冒泡的,依旧阻断了Son上的React Event。

同时如果我们把dad上的stopPropagation remove掉我们会看到如下结果:

"native Event son is fired"
"native Event Dad is fired"
"native Event GrandPa is fired"
"React Event Son is fired"
"React Event Dad is fired"
"React Event grandpa is fired"

这就说明React的合成时间是在原生事件冒泡到最顶层组件结束后才创建和冒泡的,也是符合React的原理,因为在是实现的时候React只是将一个Event listener 挂在了最顶层的组件上,其内部一套自己的机制进行事件的管理。

浅析React之事件系统(一)

相关文章

  • 浅析React之事件系统(二)

    上篇文章中,我们谈到了React事件系统的实现方式,和在React中使用原生事件的方法,那么这篇文章我们来继续分析...

  • 浅析React之事件系统(一)

    大家周末好,2016年的最后几篇文章开始写到了React的一些东西,那么最近就来一些图表君对于React的简单总结...

  • React事件系统之touch

    之前写在react上写dom事件大部分都是PC端,基本是click事件,没遇到过什么问题。这次要写一个移动端的项目...

  • 15、React系列之--React 事件系统

    版权声明:本文为博主原创文章,未经博主允许不得转载。 PS:转载请注明出处作者:TigerChain地址:http...

  • React事件系统

    基本使用 React事件名采用驼峰式写法,而不是小写 React事件不能直接rerurn false来阻止默认行为...

  • React的事件

    React事件系统 事件处理程序通过 合成事件(SyntheticEvent)的实例传递,SyntheticEve...

  • 《深入React技术栈》学习笔记Ⅱ

    一、React 事件系统React 基于 Virtual DOM 实现了一个 SyntheticEvent(合成事...

  • React学习之漫谈React

    事件系统 合成事件的绑定方式 Test 合成事件的实现机制:事件委派和自动绑定。 React合成事件系统的委托机制...

  • React事件系统详解

    为了更好的阅读体验,你可以点击这里查看github原文 阅读前的测试 想点↗x关闭的同学,先花一分钟进行测试,再来...

  • react事件系统原理

    1 .我们编写的click事件,最终会被转换城fiber对象2 .fiber对象上的memoizedProps和p...

网友评论

    本文标题:浅析React之事件系统(二)

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