Skip to content

Instantly share code, notes, and snippets.

@barinbritva
Last active August 10, 2020 13:26
Show Gist options
  • Save barinbritva/14425c05da1047aa3b5c to your computer and use it in GitHub Desktop.
Save barinbritva/14425c05da1047aa3b5c to your computer and use it in GitHub Desktop.
React detect clicked element
protected componentWillMount(): void {
document.addEventListener('click', this.toggle);
}
protected componentWillUnmount(): void {
document.removeEventListener('click', this.toggle);
}
private toggle = (event): void => {
event.preventDefault();
let component = ReactDOM.findDOMNode(this.refs['dropDown']);
if (event.target === component || component.contains(event.target)) {
// inside element
} else {
// outside element
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment