The SyntheticEvent is pooled. This means that the SyntheticEvent object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.
最佳答案
事件池 - React 使用 SyntheticEvent,它是 native 浏览器事件的包装器,因此它们在不同的浏览器中具有一致的属性。除非我们使用 nativeEvent 属性来获取底层浏览器事件,否则我们在任何 react-app 中拥有的事件处理程序实际上都是通过 SyntheticEvent 实例传递的。
包装 native 事件实例可能会导致性能问题,因为创建的每个合成事件包装器还需要在某个时候进行垃圾收集,这在 CPU 时间方面可能非常昂贵。
React 通过分配一个合成实例池 来处理这个问题。每当触发事件时,它都会从池中获取一个实例并填充其属性并重新使用它。当事件处理程序完成运行时,所有属性都将被取消,合成事件实例将被释放回池中。因此,提高了性能。
关于javascript - React 中的事件池是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114196/