简体   繁体   中英

react-responsive-modal : how to prevent closing of modal when we click outside the div

I am using react-responsive-modal for implementing my modal. One issue i am facing is that my modal gets closed when i click anywhere on the screen, outside my modal div. I want my modal to be closed only when i click the cancel or close button. I found onOverlayClick function props and i used it to apply preventDefault but it was of no use.

can anyone please suggest a way to implement this feature. thanks in advance.

class CustomModal extends Component{
constructor(props) {
    super(props);
}

onOverlay = (e) =>{
  e.preventDefault();
}

render(){
    const { props } = this;

    return(
        <Modal open={props.open} onClose={props.onCloseModal} 
         onOverlayClick={e =>this.onOverlay(e)} center>
        <div className="on-setting-modal">
          <h6>"are you sure ?"</h6>
          <div className="on-setting-modal-buttons">
            {
                !!props.showCancel &&
                <button onClick={props.onCloseModal} >Cancel</button>
            }
            <button onClick={props.onOkModal} >OK</button>
          </div>
        </div>
      </Modal>
  )
 }
}

export default CustomModal

Your problem can be solved by using the property called closeOnOverlayClick={false} this makes the modal to be not closed when we click on the overlay

You can add closeOnOverlayClick={false} in your Modal component

Just set onOverlayClick to false, it will resolve the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM