I'm listening to click bubbled events on window and I want to check if the click occurred within a a specific element in order to dispatch (or not) an action. The inerestinc part of my component is the following one:
const handleClickOutsideSearchArea = (event: Event) => { if (!textAreaRef?.current?.contains(event.target)) { dispatch({type: <MY-ACTION>}); } }; window.addEventListener('click', handleClickOutsideSearchArea); this code works, BUT event.target is an EventTarget whether contains needs a Node, a child of EventTarget, then Flow complaints about it.
How could I fix this? Is there a way to get the node from the event?
textAreaRef.currentalready references the correspondent DOM's node, use it instead oftarget(Also take a look at event delegation in React)window. I'll update the question in order to give it a better context