Skip to content

Instantly share code, notes, and snippets.

@ColtHands
Created August 16, 2020 18:55
Show Gist options
  • Save ColtHands/070379ad1ecf9d3d825f89a2b0ab6140 to your computer and use it in GitHub Desktop.
Save ColtHands/070379ad1ecf9d3d825f89a2b0ab6140 to your computer and use it in GitHub Desktop.
React click outside of component
componentDidMount() {
document.addEventListener('click', this.clickOutside.bind(this), true);
}
componentWillUnmount() {
document.removeEventListener('click', this.clickOutside.bind(this), true);
}
clickOutside(event) {
const domNode = ReactDOM.findDOMNode(this);
if ((!domNode || !domNode.contains(event.target))) {
this.setState({
visible : false
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment