Listen for bootstrap modal close event in reactjs

末鹿安然 提交于 2019-12-12 01:57:53

问题


is there a way I can listen for a bootstrap modal closing? In pure jQuery I would do something like:

$('#myModal').on('hidden.bs.modal', function () {
     // do something…
})

However, what would be the appropriate way to do it in react?

Thanks in advance!


回答1:


If you are using react-bootstrap's modals You can pass the handler function via onHide or onExit prop.




回答2:


the below code will give you the idea, as You can control whether the modal is shown using this.setState({showModal: true}) to achieve the equivalent of what you asked for in your post in the example below

const ControlledModalExample = React.createClass({

getInitialState(){
  return { showModal: false };
},

close(){
  this.setState({ showModal: false });
},

open(){
  this.setState({ showModal: true });
},

render() {
  return (
    <div>
      <Button onClick={this.open}>
        Launch modal
      </Button>

    <Modal show={this.state.showModal} onHide={this.close}>
      <Modal.Header closeButton>
        <Modal.Title>Modal heading</Modal.Title>
      </Modal.Header>
      <Modal.Body>
        <div>Modal content here </div>
      </Modal.Body>
      <Modal.Footer>
        <Button onClick={this.close}>Close</Button>
      </Modal.Footer>
    </Modal>
   </div>
  );
 }
});

you can get more information from the below doc url,

http://react-bootstrap.github.io/components.html#modals



来源:https://stackoverflow.com/questions/39212827/listen-for-bootstrap-modal-close-event-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!