Skip to content

Instantly share code, notes, and snippets.

@knowbody
Created November 4, 2015 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knowbody/aa21d95f80bc0f5e3954 to your computer and use it in GitHub Desktop.
Save knowbody/aa21d95f80bc0f5e3954 to your computer and use it in GitHub Desktop.
React onClickOutside
componentDidMount() {
document.body.addEventListener('click', this.handleBodyClick);
}
componentWillUnmount() {
document.body.removeEventListener('click', this.handleBodyClick);
}
handleBodyClick(e) {
const area = this.refs.item.getDOMNode();
if (!area.contains(e.target)) {
console.log('body click')
}
}
// in the onClick event use e.stopPropagation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment