简体   繁体   English

React 合成事件与原生 Javascript 事件?

[英]React Synthetic Events vs Native Javascript Events?

I have a hard time understanding react synthetic events.我很难理解反应合成事件。

The way I debug with native javascript events is something like this我用原生 javascript 事件调试的方式是这样的

window.addEventListener('mousemove',
  function(event){
    console.log(event);
})

If I move my mouse, and check the console, I can see the events properties such as target and other useful properties such as innerHTML , nextElementSibling , lastChild etc.如果我移动鼠标并检查控制台,我可以看到事件属性,例如target和其他有用的属性,例如innerHTMLnextElementSiblinglastChild等。

在此处输入图片说明

I have a simple react app below.我在下面有一个简单的反应应用程序。 Its an input field with a submit button.它是一个带有提交按钮的输入字段。 If something is entered in the input when submitted, an alert pops up.如果提交时在输入中输入了某些内容,则会弹出警报。 Otherwise nothing否则什么都没有

 // React Synthetic Events class AddOption extends React.Component { handleAddOption(e){ e.preventDefault(); console.log(e); const option = e.target.elements.option.value; // < HOW WOULD I FIND THIS IN THE DEBUGGER? if (option){ alert("something was entered in"); } } render() { return ( <div> <form onSubmit={this.handleAddOption}> <input type="text" name="option" /> <button>React Submit</button> </form> </div> ) } } ReactDOM.render(<AddOption />, document.getElementById('app'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"><!-- reactapp -->

What I don't get is if I inspect the page it doesn't give that useful information like I get with native events.我不明白的是,如果我检查页面,它不会像我使用本机事件那样提供有用的信息。

The issue is I referenced in my reactapp (following a tutorial) to use e.target.element.options.value .问题是我在我的 reactapp 中引用(按照教程)使用e.target.element.options.value I have no idea how someone would find that out in the debugger.我不知道有人会如何在调试器中发现这一点。 I don't see a reference to anything dubbed element in any of the long nested chain of prototype properties under synthetic event's target .在合成事件的target下的任何原型属性的长嵌套链中,我​​没有看到对任何配音element的引用。 I tried CTRL+F but I don't think chrome supports searching nested prototypes我试过CTRL+F但我认为 chrome 不支持搜索嵌套原型

Am I not understanding something about things happening in the virtual DOM / react in general?我是不是不了解虚拟 DOM 中发生的事情/一般反应?

在此处输入图片说明

per li357 comment, adding e.persist() right before the console.log(e) statement in original post shows this in the debugger.根据 li357 评论,在原始帖子中的console.log(e)语句之前添加e.persist()在调试器中显示这一点。 You can see the native javascript event's properties include target , element and the defined option from react你可以看到原生 javascript 事件的属性包括targetelement和 react 中定义的option

在此处输入图片说明

You can use e.nativeEvent to get access to the native event and from there on to the elements.option.value property:您可以使用e.nativeEvent访问本机事件并从那里访问elements.option.value属性:

// React Synthetic Events
class AddOption extends React.Component {
    handleAddOption(e){
      e.preventDefault();
      console.log(e);

      const option = e.target.elements.option.value; // < HOW WOULD I FIND THIS IN THE DEBUGGER?
      // > YOU CAN USE e.nativeEvent TO GET ACCESS TO THE NATIVE EVENT:
      console.log(e.nativeEvent);
      console.log(e.nativeEvent.target.elements.option.value); // < HERE IS YOUR VALUE

      if (option){
        alert("something was entered in");
      }
    }
    render() {
      return (
        <div>
          <form onSubmit={this.handleAddOption}>
            <input type="text" name="option" defaultValue={"Toy"}/>
            <button>React Submit</button>
          </form>
        </div>
      )
    }
}
ReactDOM.render(<AddOption />, document.getElementById('root'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM