Created
November 4, 2015 16:10
-
-
Save knowbody/aa21d95f80bc0f5e3954 to your computer and use it in GitHub Desktop.
React onClickOutside
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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