Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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