Proper use of useEffect with KeyboardEvent eventListener

If there is state in my onKeyDown function which is set in document.addEventListener("keydown", onKeyDown), how would i keep my state within onKeyDown from getting stale? Should it be added as a dependency in my useEffect? For example:
useEffect(() => {
document.addEventListener("keydown", onKeyDown);

return () => document.removeEventListener("keydown", onKeyDown);
}, [myState]);
useEffect(() => {
document.addEventListener("keydown", onKeyDown);

return () => document.removeEventListener("keydown", onKeyDown);
}, [myState]);
4 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cucaracha
cucarachaOP3y ago
does it matter if i define it outside or inside the useeffect? i guess it gets redfined on re-render but thats not expensive right?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cucaracha
cucarachaOP3y ago
Sweet thanks
Want results from more Discord servers?
Add your server