简体   繁体   English

如何检查 dom 的反应 onKeyDown

[英]How to check dom for react onKeyDown

Im creating examples of things that would cause trouble for keyboard only users.我正在创建一些会给仅使用键盘的用户带来麻烦的示例。 I have an element that I created like this:我有一个像这样创建的元素:

<div id="role_tab_div_with_react_onclick_has_keydown" role="tab" onClick={() => activateDialog()} tabIndex="0" onKeyDown={() => activateDialog()}>
    <div class="btn-label">Click me (role=tab - has onkeyDown)</div>
</div>

But, when rendered to the DOM, the onKeyDown is not present even thought it is now keyboard activatable.但是,当呈现到 DOM 时,即使认为它现在可以键盘激活,onKeyDown 也不存在。

<div id="role_tab_div_with_react_onclick_has_keydown" role="tab" tabindex="0">
   <div class="btn-label">Click me (role=tab - has onkeyDown)</div>
</div>

So, my check to see if the element already has a onkeydown event:所以,我检查元素是否已经有一个 onkeydown 事件:

if (el.hasAttribute('onKeyPress' || 'onKeyDown'))

is not finding it.没有找到它。

Is there a way to determine if it does have a react version of onKeyDown present?有没有办法确定它是否存在 onKeyDown 的反应版本?

React handles event listeners a little differently than you'd expect. React 处理事件侦听器的方式与您预期的略有不同。 As you pointed out, it does not attach a listener directly to the DOM element it creates/manages.正如您所指出的,它不会将侦听器直接附加到它创建/管理的 DOM 元素。 Instead, it attaches a listener higher up the DOM hierarchy and uses the information about the source element to determine which React component manages that element and by extension where to find registered callbacks.相反,它会在 DOM 层次结构的更高层附加一个侦听器,并使用有关源元素的信息来确定哪个 React 组件管理该元素,并通过扩展确定在哪里可以找到已注册的回调。 You may find this article helpful if you're looking for more of the nitty-gritty: https://dev.to/romaintrotard/under-the-hood-of-event-listeners-in-react-4g01 .如果您正在寻找更多细节,您可能会发现这篇文章很有帮助: https : //dev.to/romaintrotard/under-the-hood-of-event-listeners-in-react-4g01

Because of the indirect way in which React listens for events you will not have as clear a way to list callbacks.由于 React 侦听事件的间接方式,您将无法清楚地列出回调。 However, there are a few options depending on your use case:但是,根据您的用例,有几个选项:

  1. If you know what function is supposed to be called as a callback then you can either add a breakpoint in the browser's debugger or add some sort of side effect to your callback that you can watch for (eg console.log() ).如果您知道应该作为回调调用什么函数,那么您可以在浏览器的调试器中添加一个断点,或者向您的回调添加某种可以观察的副作用(例如console.log() )。
  2. If you know the intended side effects of the callback but don't have the ability to find or change the callback function itself, you can look for those instead of the handler itself.如果您知道回调的预期副作用,但无法找到或更改回调函数本身,则可以查找这些而不是处理程序本身。
  3. If the code is effectively a black box to you (which it sounds like is the case based on your comment) then there is the ability to look at the React objects attached to that DOM node.如果代码对您来说实际上是一个黑匣子(根据您的评论听起来就是这种情况),那么就可以查看附加到该 DOM 节点的 React 对象。 This is not part of React's published API and therefore is subject to more frequent or dramatic change than JSX, core components' props, etc.. You may find that some websites have the React internals for a node under __reactInternalInstance$<some suffix> and others have __reactProps$<some suffix> .这不是 React 发布的 API 的一部分,因此比 JSX、核心组件的 props 等更频繁或更剧烈的变化。你可能会发现一些网站在__reactInternalInstance$<some suffix>下有节点的 React 内部结构和其他人有__reactProps$<some suffix> I would only recommend using it as a last resort and with extreme caution, but it is an option.我只建议将它用作最后的手段,并极其谨慎,但它是一种选择。

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

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