📌  相关文章
📜  将事件处理程序传递给 useEffeect - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:47.055000             🧑  作者: Mango

代码示例1
const App =() => {
  const [userText, setUserText] = useState("");

  useEffect(() => {
    const handleUserKeyPress = event => {
      const { key, keyCode } = event;

      if (keyCode === 32 || (keyCode >= 65 && keyCode <= 90)) {
        setUserText(`${userText}${key}`);
      }
    };

    window.addEventListener("keydown", handleUserKeyPress);

    return () => {
      window.removeEventListener("keydown", handleUserKeyPress);
    };
  }, [userText]); // ESLint will yell here, if `userText` is missing

  return (
    

Feel free to type!

{userText}
); } ReactDOM.render(, document.getElementById("root"));