How to know that user has clicked outside our react app which is pointed to
<div id="root">
(I'm having extra space to click outside root div)
I've tried the below code
import ReactDOM from 'react-dom';
// ... ✂
componentDidMount() {
document.addEventListener('click', this.handleClickOutside, true);
}
componentWillUnmount() {
document.removeEventListener('click', this.handleClickOutside, true);
}
handleClickOutside = event => {
const domNode = ReactDOM.findDOMNode(this);
if (!domNode || !domNode.contains(event.target)) {
this.setState({
visible: false
});
console.log("clicked outside")
}
}
But then, even if I clicked inside some child popup component under parent, it is showing as "clicked outside"
If I click anywhere inside the app (including children component), it should not say, "clicked outside"
So is there any way to know that the user clicked outside the complete app itself?