Created
August 16, 2020 18:55
-
-
Save ColtHands/070379ad1ecf9d3d825f89a2b0ab6140 to your computer and use it in GitHub Desktop.
React click outside of component
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.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