简体   繁体   English

在页面加载时自动在 React JS 应用程序中打开模态

[英]Open Modal in React JS Application on Page Load automatically

Below is the App class component in which I want to open Modal once the page gets loaded completely.下面是App类组件,一旦页面完全加载,我想在其中打开 Modal。

Also I tried using componentDidMount() lifecycle method but it failed.我也尝试使用componentDidMount()生命周期方法,但失败了。

class App extends React.Component {
    constructor(props) {
        super(props);
        this.handleSHow = this.handleShow.bind(this);
        this.state = {
            modalState : true
        }
    }

    handleShow = () => {
        this.setState(() => {
            return {
                modalState : false
            };
        });
    }

    render() {
        console.log(this.state.modalState)
        return (
            <div>
                <h1 className="main-title">My Profile</h1>
                <MainContent />
            </div>
        );
    }
}

export default App;

Assuming that you're using Bootstrap Modal , here is a basic example:假设您使用的是Bootstrap Modal ,这是一个基本示例:

 class App extends React.Component { constructor(props) { super(props); this.state = { modalState: true }; this.handleShow = this.handleShow.bind(this); } handleShow() { this.setState({ modalState: !this.state.modalState }); } render() { return ( <div> <div className={"modal fade" + (this.state.modalState ? " show d-block" : " d-none")} tabIndex="-1" role="dialog"> <div className="modal-dialog" role="document"> <div className="modal-content"> <div className="modal-header"> <h5 className="modal-title">My Profile</h5> <button type="button" className="close" onClick={this.handleShow}> <span>&times;</span> </button> </div> <div className="modal-body">...</div> <div className="modal-footer"> <button type="button" className="btn btn-secondary" onClick={this.handleShow}>Close</button> <button type="button" className="btn btn-primary">Save changes</button> </div> </div> </div> </div> </div> ); } } ReactDOM.render(<App />, document.getElementById('root'));
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://unpkg.com/react@16/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <div id="root"></div>

And if you're using react-bootstrap , use like this:如果您使用的是react-bootstrap ,请像这样使用:

 class App extends React.Component { constructor(props) { super(props); this.state = { show: true }; this.handleClose = this.handleClose.bind(this); this.handleShow = this.handleShow.bind(this); } handleClose (){ this.setState({ show: false }); }; handleShow (){ this.setState({ show: true }); }; render() { return( <div> <ReactBootstrap.Button variant="primary" onClick={this.handleShow}>Launch modal</ReactBootstrap.Button> <ReactBootstrap.Modal show={this.state.show} onHide={this.handleClose}> <ReactBootstrap.Modal.Header closeButton> <ReactBootstrap.Modal.Title>My Profile</ReactBootstrap.Modal.Title> </ReactBootstrap.Modal.Header> <ReactBootstrap.Modal.Body>...</ReactBootstrap.Modal.Body> <ReactBootstrap.Modal.Footer> <ReactBootstrap.Button variant="secondary" onClick={this.handleClose}>Close</ReactBootstrap.Button> <ReactBootstrap.Button variant="primary">Save Changes</ReactBootstrap.Button> </ReactBootstrap.Modal.Footer> </ReactBootstrap.Modal> </div> ); } } ReactDOM.render(<App />, document.getElementById('root'));
 <script src="https://unpkg.com/react@16/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://unpkg.com/react-bootstrap@next/dist/react-bootstrap.min.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <div id="root"></div>

For the correct answer, the Modal code is also required.要获得正确答案,还需要模态代码。 We need to know what kind of modal you are using and how does it's toggle mechanism works.我们需要知道您使用的是哪种模态以及它的切换机制是如何工作的。

However, looking at your code I have found a typo.但是,查看您的代码我发现了一个错字。

this.handleSHow = this.handleShow.bind(this);
Here in this.handleSHow the H is in Caps.this.handleSHowH是如何用大写this.handleSHow的。 So if you are calling the method to trigger the Modal's open state like so this.handleShow() then it will not work.因此,如果您像this.handleShow()那样调用方法来触发 Modal 的打开状态,那么它将不起作用。

Either fix the typo or call the method like this: this.handleSHow() .要么修复拼写错误,要么像这样调用方法: this.handleSHow()

Edit: Looking again at your code I just realized you don't need this.handleSHow = this.handleShow.bind(this);编辑:再看你的代码我才意识到你不需要this.handleSHow = this.handleShow.bind(this); this line because you are using arrow function syntax to declare your method ie, handleShow = () => .这一行是因为您使用箭头函数语法来声明您的方法,即handleShow = () => Arrow functions automatically bind the this context to your function from outer scope which in this case is your App Component itself so you don't need to .bind(this) in your constructor.箭头函数会自动将this上下文从外部作用域绑定到您的函数,在这种情况下是您的App组件本身,因此您不需要在构造函数中使用.bind(this)

If you still want to keep this.handleSHow = this.handleShow.bind(this);如果你还想保留this.handleSHow = this.handleShow.bind(this); then you have to change your arrow function handleShow = () => to handleShow() { ... } syntax.那么您必须将箭头函数handleShow = () =>更改为handleShow() { ... }语法。

You could do something like this你可以做这样的事情

class App extends React.Component {
constructor(props){
  super(props);
  this.handleSHow = this.handleShow.bind(this);
  this.state = {
    modalState : false
  }
}

componentDidMount(){
  this.setState({
    modalState : true
  }) 
}

render()
{
  console.log(this.state.modalState)
  return(
      <div>
      {this.state.modalState ? <YourModal/> : null}
      <h1 className="main-title">My Profile</h1>
      <MainContent />
      </div>
      )
}
}

export default App;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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