Answer the question
In order to leave comments, you need to log in
How to close a popup on click outside of it in a class component?
There are two buttons on the page, by clicking on each of them a popup appears with a translucent darkened background. In the App.js state, there are two variables with a value false
for each modal, which actually change on click on these buttons through this.setState
to true
, for their appearance by conditional rendering. How to make a click handler on an area outside the modal in React so that it closes?
Answer the question
In order to leave comments, you need to log in
In parent:
state = {
modal: false,
}
hideModal = () => {
this.setState({ modal: false })
}
render() {
return (
...
{this.state.modal ? <Modal close={this.hideModal} /> : null}
...
);
}
return (
<div
className="modal-overlay"
onClick={e => (e.currentTarget === e.target) && props.close()}
>
<div className="modal-content">
...
</div>
</div>
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question