防止事件侦听器多次触发事件
我正在使用此事件侦听器来触发一个事件,该事件基本上更新了 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);
}
}
});
}, []);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免 React 从一个 useEffect 钩子添加多个事件监听器,您可以返回一个清理函数来删除 useEffect 钩子中的事件监听器。每当组件卸载时,清理函数就会运行。例如:
这非常适合删除事件侦听器。您可以参考 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:
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
为了确保监听器只添加一次,我做的一件事是在每个 addListener 之前调用removeEventListener
In order to make sure listeners are only added once, one thing I do is call removeEventListener before every addListener
嗯,这对我有用!感谢以上两位贡献者!
well this works for me! thanks to both contributors above!