You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
I came across a situation where I need to have an event listener on the window and also need access to a piece of state in a component when that event is handled.
I was hoping to get your feedback on how you might approach this problem? How would go going about deciding which of these, or another option, to implement. Is there a way you think about such problems that inform your decisions?
The problem: When the event listener is attached to the window the state becomes stale, and we need a way to access it.
I came up with two solutions and could also make a case for a third using useRef():
useEffect where I remove then add back the event listener when that piece of state changes.
const[activeColor,setActiveColor]=useState('')consthandleKeyDown=useCallback(({ key })=>{if(Object.keys(keyColorDictionary).includes(key)){// Color that matched the key pressconstselectedColor=keyColorDictionary[key];// if the key is same as activeColor, turn it off// otherwise set it to selectedColorsetActiveColor(selectedColor===activeColor ? "" : selectedColor);}},[activeColor]);useEffect(()=>{// setup event listenerwindow.addEventListener("keydown",handleKeyDown);return()=>{// teardown event listenerwindow.removeEventListener("keydown",handleKeyDown);};},[handleKeyDown]);
Pass a callback to the event listener that accesses the state needed.
const[activeColor,setActiveColor]=useState('')consthandleKeyDown=({ key },setActiveColor)=>{if(Object.keys(keyColorDictionary).includes(key)){// Color that matched the key pressconstselectedColor=keyColorDictionary[key];// if the key is same as activeColor, turn it off// otherwise set it to selectedColorsetActiveColor(selectedColor===activeColor ? "" : selectedColor);}};useEffect(()=>{// pass setActiveColor to keyDown handler to give access to current stateconsthandleKeyDownAndSetActiveColor=e=>handleKeyDown(e,setActiveColor);// setup event listenerwindow.addEventListener("keydown",handleKeyDownAndSetActiveColor);// teardown event listenerreturn()=>{window.removeEventListener("keydown",handleKeyDownAndSetActiveColor);};},[]);
The text was updated successfully, but these errors were encountered:
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hi Kent!
I came across a situation where I need to have an event listener on the
window
and also need access to a piece of state in a component when that event is handled.I was hoping to get your feedback on how you might approach this problem? How would go going about deciding which of these, or another option, to implement. Is there a way you think about such problems that inform your decisions?
Thanks!
Codesandbox Demo:
The problem: When the event listener is attached to the window the state becomes stale, and we need a way to access it.
I came up with two solutions and could also make a case for a third using
useRef()
:useEffect
where I remove then add back the event listener when that piece of state changes.The text was updated successfully, but these errors were encountered: