DEV Community

Discussion on: How to detect click outside in a React component

Collapse
 
aderchox profile image
aderchox

One can do this even without using ref. E.g.:

<ComponentName className={
    `${style.someclass} anotherNonMangledClass`
}/>
Enter fullscreen mode Exit fullscreen mode

and then just use the e.target.closest(".anotherNonMangledClass") to check from children towards parent (in the reverse direction of yours).