[英]How to open bootstrap modal in react component?
有一个反应组件。 需要在页面加载时打开引导模式。 尝试使用 jQuery 和简单的 javascript 但没有成功。
反应组件:
import React, { Component } from 'react'
import $ from 'jquery'
class Login extends Component {
componentDidMount() {
// here I want to open modal
}
render() {
return (
<div className="modal fade WelcomeModal" id="WelcomeModal" tabIndex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div className="modal-dialog modal-dialog-centered" role="document">
<div className="modal-content">
<div className="modal-header">
<div className="camera-box">
<img alt="" src="/img/yellow-logo.svg"/>
<h5 className="modal-title" id="exampleModalLabel">Welcome to the Cozy App!</h5>
</div>
</div>
<div className="modal-body">
<p className="text-center">On the following screens weʼll ask you to register the Cozys in your home & adjust your temperature settings. Youʼll need the following information:</p>
</div>
</div>
</div>
</div>
)
}
}
看看这个codepen ,它展示了如何在不需要 jQuery 的情况下将 Bootstrap 类与 React 结合起来
toggleModal = () => this.setState({
showLogin: !this.state.showLogin
})
首先,不要在反应中使用 JQUERY。 您只需要设置显示或隐藏模式的状态。 就是这样。
<div onClick={this.setState({showModal: true})}
在您的公共文件夹 index.htmlhttps://getbootstrap.com/docs/4.0/getting-started/introduction/ 中包含 Js cdn
然后像常规一样调用引导程序模态, https://getbootstrap.com/docs/4.0/components/modal/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.