简体   繁体   English

为什么 Dose React 使用合成事件?

[英]Why Dose React Use Synthetic Events?

According to the docs, a react synthetic event is a根据文档,反应合成事件是

a cross-browser wrapper around the browser's native event.围绕浏览器的本机事件的跨浏览器包装器。 It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.它与浏览器的本机事件具有相同的接口,包括 stopPropagation() 和 preventDefault(),除了事件在所有浏览器中的工作方式相同。

Why is it important that the events work the same in every browser if their interface is always the same (as implied by 'It has the same interface as the browser's native event' )?如果事件的界面始终相同(正如“它与浏览器的本机事件具有相同的界面”所暗示的那样),为什么事件在每个浏览器中的工作方式相同很重要? Dose the fact that React uses event delegation make this nesessary somehow? React 使用事件委托的事实是否使这变得有必要?

A couple of years ago, developers needed to use something like this to be sure that one even would work on every browser.几年前,开发人员需要使用类似的东西来确保一个甚至可以在每个浏览器上工作。


function addEvent(evnt, elem, func) {
   if (elem.addEventListener)  // W3
      elem.addEventListener(evnt,func,false);
   else if (elem.attachEvent) { // Internet Explorer
      elem.attachEvent("on"+evnt, func);
   }
   else { // other browsers
      elem["on"+evnt] = func;
   }
}

Most of the times the problems were caused by Internet Explorer.大多数情况下,问题是由 Internet Explorer 引起的。 React (the same way as jQuery does) that care of it for us. React(与 jQuery 一样)为我们关心它。 Because still today most companies are still using IE10/11 we need to pay attention in writing the correct code to attach an event.因为今天大多数公司仍在使用 IE10/11,我们需要注意编写正确的代码来附加事件。

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

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