Answer the question
In order to leave comments, you need to log in
How to correctly handle a click outside of a component in react js?
Current code:
componentWillMount() {
document.addEventListener('click', this.onClickOuterModal, false);
}
componentWillUnmount() {
document.removeEventListener('click', this.onClickOuterModal, false);
}
onClickOuterModal = (event) => {
const modal = document.getElementsByClassName('modal');
if (modal !== event.target) {
this.props.closeModal();
}
};
Answer the question
In order to leave comments, you need to log in
if (!modal.contains(event.target)) {
this.props.closeModal();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question