简体   繁体   English

事件处理程序在反应中无法正常工作

[英]Event handler does not work properly in react

I have this code in HomePage component and when pressing arrow down/up key, nothing happens.我在 HomePage 组件中有此代码,当按下箭头向下/向上键时,没有任何反应。 And I just realized that eventhandler works properly when pressing arrow keys with ctrl.而且我刚刚意识到使用 ctrl 按下箭头键时事件处理程序可以正常工作。 I searched everywhere, but found nothing.我到处寻找,但一无所获。 Can anyone help, please?有人可以帮忙吗? My browser is Chrome v.96.+ React version 17.0.2, node v.16.13.1我的浏览器是 Chrome v.96.+ React 版本 17.0.2,节点 v.16.13.1

 const HomePage = () => {
  const keyPressHandler = (e) => {
    if (e.repeat) return;
    if (e.key === "ArrowDown") {
      window.scrollBy(0, window.innerHeight);
    }
    if (e.key === "ArrowUp") {
      window.scrollBy(0, -window.innerHeight);
    }
  };

  return (
    <Container tabIndex="0" onKeyDown={keyPressHandler}>
      <div
        style={{ width: "100%", height: "100vh", backgroundColor: "green" }}
      />
      <div
        style={{ width: "100%", height: "100vh", backgroundColor: "gray" }}
      />
      <div style={{ width: "100%", height: "100vh", backgroundColor: "red" }} />
    </Container>
  );
};

I fixed this creating new project and pasting this code there.我修复了这个创建新项目并将此代码粘贴到那里的问题。 I had some issues while installing react-icons dependencies and I stopped it manually.我在安装 react-icons 依赖项时遇到了一些问题,我手动停止了它。 I think I get an error there我想我有一个错误

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

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