You can put the handleKeyUp
function inside of the function given to useEffect
(which is the recommended way of doing it according to the official documentation) and only add the listener and return a cleanup function when collapsed
is false.
useEffect(() => {
if (collapsed) {
return;
}
function handleKeyUp(event) {
switch (event.key) {
case "Escape":
setCollapsed(true);
break;
}
}
window.addEventListener("keyup", handleKeyUp);
return () => window.removeEventListener("keyup", handleKeyUp);
}, [collapsed]);