反应合成事件奇怪的行为

React Synthetic Event strange behavior

我搜索了其他答案, 最接近我正在处理的问题。

但我的问题仍然存在。我无法访问合成事件的属性。喜欢shiftKey

在某些情况下,我使用的是 onClick handler that comes packaged with react-d3-tree library

我以为是这个库出了问题。但在点击处理程序中,我能够将事件打印到控制台,并且我可以看到 shiftKey 参数已按预期设置。但是每次我尝试访问 event.shiftKey 我都会收到错误消息:

index.js:1452 Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property `shiftKey` on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist().

查看我的代码示例。

更新:回购以复制问题https://github.com/mwilde345/reactBrokenClick

不应依赖对象在控制台中的显示方式。对象在 JavaScript 中通过引用传递。如果对象内部在某个时候更新,它们将在控制台中更新。

persist() 创建事件对象的副本,因此它应该在调用时作为快照出现在控制台中。 persist()出现问题意味着它出了问题。

问题是由第三方组件引起的,Tree from react-d3-tree。异步使用事件对象,persist()在用户代码中不会有理想的结果,而是应该在Tree组件中同步调用persist()