【问题标题】:How to open bootstrap modal in react component?如何在反应组件中打开引导模式?
【发布时间】:2018-10-11 06:03:38
【问题描述】:

有一个反应组件。需要在页面加载时打开引导模式。尝试使用 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>
    )
  }
}

【问题讨论】:

标签: javascript jquery reactjs bootstrap-modal react-bootstrap4-modal


【解决方案1】:

看看这个codepen,它展示了如何在不需要 jQuery 的情况下将 Bootstrap 类与 React 结合起来

toggleModal = () => this.setState({
              showLogin: !this.state.showLogin
          })

【讨论】:

  • 这很好,但是如果您单击模态本身,它就会消失
【解决方案2】:

首先,不要在反应中使用 JQUERY。 您只需要设置显示或隐藏模式的状态。就是这样。

<div onClick={this.setState({showModal: true})}

【讨论】:

    【解决方案3】:

    您不需要为此目的使用 Jquery,您只需使用引导 css 类,这些类依次显示或隐藏模式。

    您可以在下面的链接中查看示例

    Link

    【讨论】:

    • 这不提供完整的引导模式功能。 Bootstrap modal 严重依赖额外的 javascript,实现焦点陷阱、身体滚动锁定和背景渲染,仅举几例。
    • @zephi :如果您需要所有动画和其他功能,请参考 reactstrap [reactstrap.github.io/components/modals/] 这提供了大多数引导组件作为反应组件。
    • 我只是指出引导模式不是一个仅 css 的解决方案。因此,您声称您需要做的就是使用 css 类可能会产生误导。
    【解决方案4】:

    在您的公共文件夹 index.html 中包含 Js cdn https://getbootstrap.com/docs/4.0/getting-started/introduction/

    然后将引导模式称为常规模式, https://getbootstrap.com/docs/4.0/components/modal/

    【讨论】:

    • 以代码格式提供它会更好,但无论如何,好的工作会继续下去。
    猜你喜欢
    • 2019-11-14
    • 2021-03-30
    • 2020-07-30
    • 2020-12-31
    • 2018-03-29
    • 2020-05-16
    • 2022-10-31
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多