简体   繁体   English

钩子 usKeyPress 和 useEventListener 之间的性能差异

[英]Performance difference between hooks usKeyPress and useEventListener

The useEventListener hook and the useKeyPress seem to have a slightly different implementation, but I'm trying to figure out which is a better tool to use for my specific use case. useEventListener钩子和useKeyPress似乎有一个稍微不同的实现,但我试图找出哪个是用于我的特定用例的更好工具。

I have a custom dropdown select menu, and I want to listen to the arrow down, up and enter keys.我有一个自定义下拉菜单 select 菜单,我想听向下箭头,向上和输入键。 My problem, or rather question, about the useKeyPress hook, is that there are two renders that happen + I'm not really sure why there is an intermediate useState.我的问题,或者更确切地说,关于 useKeyPress 钩子的问题是发生了两个渲染 + 我不太确定为什么会有中间 useState。

For instance, using the useKeyPress hook, if a user click on a down arrow, the event listeners fire off twice, one would return true, and immediately return false onKeyUp:例如,使用 useKeyPress 钩子,如果用户单击向下箭头,事件监听器会触发两次,一个会返回 true,然后立即返回 false onKeyUp:

  useEffect(() => {
    window.addEventListener('keydown', downHandler);
    window.addEventListener('keyup', upHandler);
    return () => {
      window.removeEventListener('keydown', downHandler);
      window.removeEventListener('keyup', upHandler);
    };
  }, []);

Also, I'm not sure why it's doing,另外,我不确定它为什么会这样,

const [keyPressed, setKeyPressed] = useState(false);

I'm just looking for some clarification on the difference between these two, and which one to use for my use case.我只是想澄清一下这两者之间的区别,以及哪一个用于我的用例。

Performance shouldn't be your primary concern as both are fast enough for nearly every use case they cover.性能不应该是您最关心的问题,因为两者对于它们涵盖的几乎所有用例都足够快。

useKeyPress is probably not well suited for your use case as it only gives you information of when a key is pressed. useKeyPress可能不太适合您的用例,因为它仅提供有关何时按下键的信息。 It doesn't let you call a handler.它不允许您调用处理程序。 It internally keeps track of the pressed state itself.它在内部跟踪按下的 state 本身。 But you likely want to call a handler, eg to change the currently selected item in your dropdown list.但是您可能想要调用处理程序,例如更改下拉列表中当前选定的项目。

Also, I'm not sure why it's doing,另外,我不确定它为什么会这样,

const [keyPressed, setKeyPressed] = useState(false);

That's simply the use case.这只是用例。 It keeps track of the pressed state of a button.它跟踪按下的按钮 state。

useEventListener is a more generic tool that lets you do the updating and state management yourself. useEventListener是一个更通用的工具,可让您自己进行更新和 state 管理。 It lets you decide what to update based on the subscribed events.它使您可以根据订阅的事件决定要更新的内容。 You could in fact rewrite useKeyPress to internally use useEventListener .实际上,您可以重写useKeyPress以在内部使用useEventListener

React works by updating state. React 通过更新 state 来工作。 If the useKeyPress hook didn't update some internal state then it would be incapable of returning an updated value reference and trigger a component rerender.如果useKeyPress钩子没有更新某些内部 state 那么它将无法返回更新的值引用并触发组件重新渲染。 useKeyPress listens for the keyDown event to toggle state to true, then listens for the keyUp event to clear that state (you don't want it stuck true. useKeyPress侦听 keyDown 事件以将 state 切换为 true,然后侦听 keyUp 事件以清除 state(您不希望它保持为真。

The useEventListener is certainly a heavier tool; useEventListener肯定是一个更重的工具; it's capable of monitoring many more events.它能够监控更多事件。 It doesn't use internal state though, but rather requires callback handlers to be passed to it to be invoked when registered event occurs.它虽然不使用内部 state,但需要将回调处理程序传递给它以在注册事件发生时调用。

For a lightweight utility the onKeyPress is useful if you just need to trigger an effect callback in a component, but for larger projects or where you want more than to know a keyPress occurred, useEventListener can handle it.对于轻量级实用程序,如果您只需要在组件中触发效果回调,则onKeyPress很有用,但对于较大的项目或您想知道发生 keyPress 的更多信息, useEventListener可以处理它。

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

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