简体   繁体   English

反应合成事件奇怪的行为

[英]React Synthetic Event strange behavior

I've scoured other answers, this one coming the closest to what I'm dealing with. 我搜寻了其他答案, 这个答案最接近我正在处理的答案。

But my issue remains. 但是我的问题仍然存在。 I cannot access the properties of a synthetic event. 我无法访问综合事件的属性。 Like shiftKey shiftKey

For some context, I'm using an onClick handler that comes packaged with react-d3-tree library 在某些情况下,我使用的是与react-d3-tree库打包在一起onClick处理程序

I thought it was just this library causing problems. 我以为这只是库引起的问题。 But in the click handler I am able to print the event to the console, and I can see the shiftKey parameter is set as expected. 但是在单击处理程序中,我可以将事件打印到控制台,并且可以看到shiftKey参数已按预期设置。 But every time I try to access event.shiftKey I get the error: 但是每次我尝试访问event.shiftKey ,都会收到错误消息:

index.js:1452 Warning: This synthetic event is reused for performance reasons. index.js:1452警告:出于性能原因,此合成事件被重用。 If you're seeing this, you're accessing the property `shiftKey` on a released/nullified synthetic event. 如果您看到此消息,那么您正在访问已发布/无效的合成事件上的属性“ shiftKey”。 This is set to null. 设置为空。 If you must keep the original synthetic event around, use event.persist(). 如果必须保留原始的合成事件,请使用event.persist()。

See my code sample. 参见我的代码示例。

Update: Repo to replicate issue https://github.com/mwilde345/reactBrokenClick 更新:回购以复制问题https://github.com/mwilde345/reactBrokenClick

The way objects appear in console shouldn't be relied on. 不应依赖对象在控制台中出现的方式。 Objects are passed by reference in JavaScript. 对象是通过JavaScript中的引用传递的。 If object internals are updated at some point, they will be updated in console. 如果对象内部在某个时刻进行了更新,则它们将在控制台中进行更新。

persist() creates a copy of event object, so it should appear in console as a snapshot at the moment it was called. persist()创建事件对象的副本,因此它在被调用时应作为快照显示在控制台中。 That the problem occurs with persist() means that something went wrong with it. persist()一起发生问题意味着这出了点问题。

The problem is caused by third-party component, Tree from react-d3-tree . 问题是由第三方组件react-d3-tree Tree引起的。 Event object is used asynchronously, persist() in user code won't have desirable results, Instead, persist() should be synchronously called in Tree component. 事件对象是异步使用的,用户代码中的persist()不会有令人满意的结果,相反,应该在Tree组件中同步调用persist()

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

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