I'm trying to use an eventListener on "keydown" that'll update my state (a boolean named showMenu).
I've placed it on the useEffect, but it does not work properly and I can't figure out why...
It re-renders the component exponentially if I put showMenu on the array at the end of my useEffect.
If I don't put showMenu at the end of my useEffect, the showMenu will only be updated once (from false to true)
Here is the code that re-renders exponentially
const [showMenu, setShowMenu] = useState(false);
useEffect(() => {
window.addEventListener("keydown", (e) => handleMenu(e));
return () => {
window.removeEventListener("keydown", (e) => handleMenu(e));
};
}, [showMenu]);
const handleMenu = (e) => {
if (e.key == "m") {
setShowMenu(!showMenu);
}
};