Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
edited body
Source Link
silicakes
  • 6.9k
  • 3
  • 32
  • 40

The problem is that EventTraget might contain either an instance of an Element, Document or Window where the latter doesn't have a contains method.

You can tackle it by checking that your EventTarget isn't window and casting your type to Element, i.e (!textAreaRef?.current?.contains(event.target as Element)) { ...

withWith some aesthetics added, this could look something like:

const { target } = event; if(target !== window) { // or !!target.contains to check for the presence of the method const containsTarget = textAreaRef?.current?.contains(target as Element); if(containsTarget) { dispatch({type: <MY-ACTION>}); } } 

The problem is that EventTraget might contain either an instance of an Element, Document or Window where the latter doesn't have a contains method.

You can tackle it by checking that your EventTarget isn't window and casting your type to Element, i.e (!textAreaRef?.current?.contains(event.target as Element)) { ...

with some aesthetics added, this could look something like:

const { target } = event; if(target !== window) { // or !!target.contains to check for the presence of the method const containsTarget = textAreaRef?.current?.contains(target as Element); if(containsTarget) { dispatch({type: <MY-ACTION>}); } } 

The problem is that EventTraget might contain either an instance of an Element, Document or Window where the latter doesn't have a contains method.

You can tackle it by checking that your EventTarget isn't window and casting your type to Element, i.e (!textAreaRef?.current?.contains(event.target as Element)) { ...

With some aesthetics added, this could look something like:

const { target } = event; if(target !== window) { // or !!target.contains to check for the presence of the method const containsTarget = textAreaRef?.current?.contains(target as Element); if(containsTarget) { dispatch({type: <MY-ACTION>}); } } 
Source Link
silicakes
  • 6.9k
  • 3
  • 32
  • 40

The problem is that EventTraget might contain either an instance of an Element, Document or Window where the latter doesn't have a contains method.

You can tackle it by checking that your EventTarget isn't window and casting your type to Element, i.e (!textAreaRef?.current?.contains(event.target as Element)) { ...

with some aesthetics added, this could look something like:

const { target } = event; if(target !== window) { // or !!target.contains to check for the presence of the method const containsTarget = textAreaRef?.current?.contains(target as Element); if(containsTarget) { dispatch({type: <MY-ACTION>}); } }