[英]How to remove the mouse up event handler after logout in react?
The route guard can prevent the url '/protected/operation' from unauthorized access.路由守卫可以防止未经授权的访问 url '/protected/operation'。
I have a mouseup
event handler in the AdminOperation component.我在 AdminOperation 组件中有一个
mouseup
事件处理程序。
The mouseup
event handler just shows "mouse up" in the console. mouseup
事件处理程序仅在控制台中显示“鼠标向上”。 My problem is that the event handler still working after I click the logout button.我的问题是单击注销按钮后事件处理程序仍在工作。
At this moment, if I browse other URL, the event handler will not work again.此时,如果我浏览其他 URL,事件处理程序将无法再次工作。
How can I remove the mouse up event handler after logout in react?注销后如何删除鼠标向上事件处理程序?
Add a cleanup method for the useEffect by returning a function from useEffect.通过从 useEffect 返回 function 为 useEffect 添加清理方法。
import React,{useEffect} from "react";
export default function AdminOperation(){
useEffect(()=>{
document.addEventListener('mouseup',mouseUp);
return () => {
document.removeEventListener('mouseup', mouseUp)
}
});
let mouseUp=(e)=>{
console.log("mouse up");
}
return(
<h3>
This is Admin. Operation page.
</h3>
)
}
documentation -https://reactjs.org/docs/hooks-effect.html#effects-with-cleanup文档 -https://reactjs.org/docs/hooks-effect.html#effects-with-cleanup
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.