简体   繁体   English

克隆一个反应的合成事件的实例

[英]Clone an instance of react's synthetic event

With vanilla JS it is possible to clone an event instance like so:使用 vanilla JS,可以像这样克隆事件实例:

const cloneEvent = event => new event.constructor(event.type, event);

Which can be used to forward event event from one DOM element to another.可用于事件事件从一个 DOM 元素转发到另一个。 For example,例如,

const buttonA = document.querySelector('#a');
const buttonB = document.querySelector('#a');

const clickHandler = (type, forwardTo) => event => {
  console.log(type, 'was clicked');
  if (forwardTo) forwardTo.dispatchEvent(cloneEvent(event));
};

buttonA.addEventListener('click', clickHandler('A', buttonB));
buttonA.addEventListener('click', clickHandler('B'));

When you click on A you will also see 2 loggings for both buttons.当您单击 A 时,您还将看到两个按钮的 2 个日志记录。

I would like to achieve the same with React's SyntheticEvent but I am running to issues presumably because SyntheticEvent have a different way of instantiation than native events.我想用 React 的SyntheticEvent实现同样的效果,但我遇到了问题,大概是因为 SyntheticEvent 的实例化方式与原生事件不同。 Here's a live demo that illustrates the problem: https://jsfiddle.net/2Lhsfceu/2/ (see the dev console logs)这是一个说明问题的现场演示: https://jsfiddle.net/2Lhsfceu/2/ (请参阅开发控制台日志)

My current solution is to clone the native event ( SyntheticEvent.nativeEvent ) as follows (updated and working live demo: https://jsfiddle.net/2Lhsfceu/1/ )我目前的解决方案是克隆本机事件( SyntheticEvent.nativeEvent )如下(更新和工作现场演示: https://jsfiddle.net/2Lhsfceu/1/

const cloneEvent = event => {
  const nativeEvent = event.nativeEvent || event;
  new nativeEvent.constructor(nativeEvent.type, nativeEvent);
}

I am wondering if there's a cleaner way or if this is the best we can do?我想知道是否有更清洁的方法,或者这是否是我们能做的最好的?

My concern is that by cloning only the native event the code base is dealing with two different types of events: the SyntheticEvent dispatched by the source event (coming from React) and the native event from forwarding.我担心的是,通过仅克隆本机事件,代码库正在处理两种不同类型的事件:由源事件(来自 React)调度的 SyntheticEvent 和来自转发的本机事件。

As soon as dispatching the native event on the DOM is exactly like scrolling manually, it should be fine.只要在 DOM 上调度本机事件就像手动滚动一样,应该没问题。

My concern is that by cloning only the native event the code base is dealing with two different types of events: the SyntheticEvent dispatched by the source event (coming from React) and the native event from forwarding.我担心的是,通过仅克隆本机事件,代码库正在处理两种不同类型的事件:由源事件(来自 React)调度的 SyntheticEvent 和来自转发的本机事件。

What React does is that it listens to all native events on the root (the <div id="#root"/> here, they get there through bubbling), then each time it receives one it creates an equivalent SyntheticEvent that gets passed in the React code. React 所做的是它侦听根上的所有本机事件(这里的<div id="#root"/> ,它们通过冒泡到达那里),然后每次接收到一个它都会创建一个等效的 SyntheticEvent 并传入反应代码。

So if you fire native events manually, React will create their SyntheticEvent equivalent as usual, and it should be fine.所以如果你手动触发原生事件,React 会像往常一样创建它们的 SyntheticEvent 等价物,应该没问题。

On very rare occasions, dispatching an event manually in Javascript will have a different effect than having a real user trigger the event.在极少数情况下,在 Javascript 中手动调度事件与让真实用户触发事件的效果不同。 See this example .请参阅此示例

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

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