防止事件侦听器多次触发事件

发布于 2025-01-12 06:06:31 字数 1418 浏览 2 评论 0原文

我正在使用此事件侦听器来触发一个事件,该事件基本上更新了 useEffect 中的 Interval 状态,并使我能够在表中上下移动。上下方法对我来说效果很好,但它会多次触发事件。导致顶部和向下事件的多次触发事件的速度很慢,并且会减慢所有应用程序的速度。

  useEffect(() => {
    console.log("HAAN MEI WUHI PAPPI HUN");
    document.addEventListener("keyup", (e) => {
      if (e.shiftKey && e.code === "ArrowRight") {
        console.log("shift + right", vjsRef.current.currentTime);
        vjsRef.current.currentTime = vjsRef.current.currentTime + 1;
      } 
      else if (e.shiftKey && e.code === "ArrowLeft") {
        console.log("fire! left", vjsRef.current.currentTime);
        vjsRef.current.currentTime = vjsRef.current.currentTime - 1;
      }
    });

    document.addEventListener("keyup", async(event) => {
      if (event.code === "ArrowUp") {
        console.log('aj mei upar ');
        await props.setSelectInterval(props.selectInterval - 1);
      }
      if (event.code === "ArrowDown") {
        if (props.selectInterval + 1 < props.row.length) {
          console.log('asmaan nichay');
          props.setSelectInterval(props.selectInterval + 1);
        }
      }
    });
  }, []);

输入图片此处描述

在此处输入图像描述

I am using this event listener to fire an event which basically updates my state of Interval in a useEffect and enabling me to go up and down in my table. The up and down approach working fine for me but its firing the event multiple times. This multiple times firing of event causing the top and down events works slow and slows over all application.

  useEffect(() => {
    console.log("HAAN MEI WUHI PAPPI HUN");
    document.addEventListener("keyup", (e) => {
      if (e.shiftKey && e.code === "ArrowRight") {
        console.log("shift + right", vjsRef.current.currentTime);
        vjsRef.current.currentTime = vjsRef.current.currentTime + 1;
      } 
      else if (e.shiftKey && e.code === "ArrowLeft") {
        console.log("fire! left", vjsRef.current.currentTime);
        vjsRef.current.currentTime = vjsRef.current.currentTime - 1;
      }
    });

    document.addEventListener("keyup", async(event) => {
      if (event.code === "ArrowUp") {
        console.log('aj mei upar ');
        await props.setSelectInterval(props.selectInterval - 1);
      }
      if (event.code === "ArrowDown") {
        if (props.selectInterval + 1 < props.row.length) {
          console.log('asmaan nichay');
          props.setSelectInterval(props.selectInterval + 1);
        }
      }
    });
  }, []);

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2025-01-19 06:06:31

为了避免 React 从一个 useEffect 钩子添加多个事件监听器,您可以返回一个清理函数来删除 useEffect 钩子中的事件监听器。每当组件卸载时,清理函数就会运行。例如:

useEffect(() => {
        const yourFunction = () => {console.log('hi')}
        document.addEventListener('keyup', yourFunction)

        return () => {
            document.removeEventListener('keyup', yourFunction)
        }
    }, [])

这非常适合删除事件侦听器。您可以参考 React useEffect hook 页面上的清理部分: https://reactjs.org/文档/hooks-effect.html

To avoid React adding multiple event listeners from the one useEffect hook, you can return a cleanup function to remove the event listener within the useEffect hook. The cleanup function will run whenever the component unmounts. eg:

useEffect(() => {
        const yourFunction = () => {console.log('hi')}
        document.addEventListener('keyup', yourFunction)

        return () => {
            document.removeEventListener('keyup', yourFunction)
        }
    }, [])

This is ideal for removing event listeners. You can refer the the cleanup section on the React useEffect hook page: https://reactjs.org/docs/hooks-effect.html

帅的被狗咬 2025-01-19 06:06:31

为了确保监听器只添加一次,我做的一件事是在每个 addListener 之前调用removeEventListener

console.log("HAAN MEI WUHI PAPPI HUN");
var foo = (e) => {
  if (e.shiftKey && e.code === "ArrowRight") {
    console.log("shift + right", vjsRef.current.currentTime);
    vjsRef.current.currentTime = vjsRef.current.currentTime + 1;
  } 
  else if (e.shiftKey && e.code === "ArrowLeft") {
    console.log("fire! left", vjsRef.current.currentTime);
    vjsRef.current.currentTime = vjsRef.current.currentTime - 1;
  }
}
document.removeEventListener("keyup", foo);
document.addEventListener("keyup", foo);

In order to make sure listeners are only added once, one thing I do is call removeEventListener before every addListener

console.log("HAAN MEI WUHI PAPPI HUN");
var foo = (e) => {
  if (e.shiftKey && e.code === "ArrowRight") {
    console.log("shift + right", vjsRef.current.currentTime);
    vjsRef.current.currentTime = vjsRef.current.currentTime + 1;
  } 
  else if (e.shiftKey && e.code === "ArrowLeft") {
    console.log("fire! left", vjsRef.current.currentTime);
    vjsRef.current.currentTime = vjsRef.current.currentTime - 1;
  }
}
document.removeEventListener("keyup", foo);
document.addEventListener("keyup", foo);
与风相奔跑 2025-01-19 06:06:31
useEffect(() => {
    var foo1 = (e) => {
      if (e.code === "ArrowUp") {
        if (props.selectInterval - 1 >= 0) {
          props.setSelectInterval(props.selectInterval - 1);
        }
      }
      if (e.code === "ArrowDown") {
        if (props.selectInterval + 1 < props.row.length) {
          props.setSelectInterval(props.selectInterval + 1);
        }
      }
    };
    document.addEventListener("keyup", foo1);
return () => {
      document.removeEventListener("keyup", foo1);
    };
  }, [props.selectInterval]);

嗯,这对我有用!感谢以上两位贡献者!

useEffect(() => {
    var foo1 = (e) => {
      if (e.code === "ArrowUp") {
        if (props.selectInterval - 1 >= 0) {
          props.setSelectInterval(props.selectInterval - 1);
        }
      }
      if (e.code === "ArrowDown") {
        if (props.selectInterval + 1 < props.row.length) {
          props.setSelectInterval(props.selectInterval + 1);
        }
      }
    };
    document.addEventListener("keyup", foo1);
return () => {
      document.removeEventListener("keyup", foo1);
    };
  }, [props.selectInterval]);

well this works for me! thanks to both contributors above!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文