简体   繁体   English

单击按钮后,Bootstrap5 模态未在 React.js 中显示

[英]Bootstrap5 modal not displaying in React.js after clicking on the button

I am using bootstrap5 to React.我正在使用 bootstrap5 进行反应。 I successfully installed the bootstrap5 in React.我在 React 中成功安装了 bootstrap5。 Now I have to show the modal on my page so I added a button and modal code on my page.现在我必须在我的页面上显示模态,所以我在我的页面上添加了一个按钮和模态代码。

Below is the code I am using for the modal.下面是我用于模态的代码。 Now I click on the button then nothing is working.现在我点击按钮然后没有任何工作。 I am not getting my modal.我没有得到我的模态。

What is the issue here?这里有什么问题?

import React from "react";

function addEmployee(){
    return(
<div className="addEmployee">
<button type="button" className="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">Add Employee</button>

<div className="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabIndex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div className="modal-dialog">
    <div className="modal-content">
      <div className="modal-header">
        <h5 className="modal-title" id="staticBackdropLabel">Modal title</h5>
        <button type="button" className="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div className="modal-body">
        ...
      </div>
      <div className="modal-footer">
        <button type="button" className="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" className="btn btn-primary">Understood</button>
      </div>
    </div>
  </div>
</div>


</div>

    )
}
export default addEmployee;

Bootstrap 5 no longer depends on jQuery so you don't need a 3rd party library library like react-bootstrap to use Bootstrap in React. Bootstrap 5 不再依赖于 jQuery ,因此您不需要像react-bootstrap这样的第三方库就可以在 React 中使用 Bootstrap。 For example you can use the data-bs- attributes in the markup, or create a functional component for the Bootstrap modal with methods to show/hide the modal...例如,您可以在标记中使用data-bs-属性,或者为 Bootstrap 模态创建一个功能组件,其中包含显示/隐藏模态的方法...

function ModalExample() {
    const modalRef = useRef()
    
    const showModal = () => {
        const modalEle = modalRef.current
        const bsModal = new bootstrap.Modal(modalEle, {
            backdrop: 'static',
            keyboard: false
        })
        bsModal.show()
    }
    
    const hideModal = () => {
        const modalEle = modalRef.current
        const bsModal= bootstrap.Modal.getInstance(modalEle)
        bsModal.hide()
    }
    
    return(
        <div className="addEmployee">
            <button type="button" className="btn btn-primary" onClick={showModal}>Add Employee</button>
            <div className="modal fade" ref={modalRef} tabIndex="-1" >
             <div className="modal-dialog">
                <div className="modal-content">
                  <div className="modal-header">
                    <h5 className="modal-title" id="staticBackdropLabel">Modal title</h5>
                    <button type="button" className="btn-close" onClick={hideModal} aria-label="Close"></button>
                  </div>
                  <div className="modal-body">
                    ...
                  </div>
                  <div className="modal-footer">
                    <button type="button" className="btn btn-secondary" onClick={hideModal}>Close</button>
                    <button type="button" className="btn btn-primary">Understood</button>
                  </div>
                </div>
              </div>
            </div>
        </div>
    )
}

Demo of both methods两种方法的演示

I would suggest you'd use react-bootstrap its a lot easier and the javascript has been done for you我建议您使用react-bootstrap它更容易,并且 javascript 已经为您完成

React-Bootstrap replaces the Bootstrap JavaScript React-Bootstrap 替换了 Bootstrap JavaScript

Each component has been built from scratch as a React component, without unneeded dependencies like jQuery.每个组件都是作为 React 组件从头开始构建的,没有像 jQuery 这样的不必要的依赖项。

To get started just install the package npm install react-bootstrap bootstrap要开始,只需安装 package npm install react-bootstrap bootstrap

look at this example看这个例子

import { Modal, Button } from "react-bootstrap";
function Example() {
  const [show, setShow] = useState(false);

  const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

  return (
    <>
      <Button variant="primary" onClick={handleShow}>
        Launch demo modal
      </Button>

      <Modal show={show} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Modal heading</Modal.Title>
        </Modal.Header>
        <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button variant="primary" onClick={handleClose}>
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>
    </>
  );
}

render(<Example />);

Gaurav Singhal's blog @ PluralSight Gaurav Singhal 的博客@PluralSight

You must use controlled components that use the internal state to set the values and change the behavior of a component.您必须使用使用内部 state 的受控组件来设置值并更改组件的行为。 Libraries like jQuery do direct DOM manipulation, whereas React components use Virtual DOM to modify the actual DOM on the browser.像 jQuery 这样的库直接进行 DOM 操作,而 React 组件使用虚拟 DOM 来修改浏览器上的实际 DOM。 Using jQuery to change your component's behavior is possible.使用 jQuery 更改组件的行为是可能的。 Still, it will lead to unexpected results and make it difficult to debug.尽管如此,它仍会导致意想不到的结果并使其难以调试。 React Bootstrap implements each component in pure React, so you don't have to worry about Bootstrap's dependency on jQuery React Bootstrap 在纯 React 中实现各个组件,所以你不必担心 Bootstrap 对 jQuery 的依赖

to check out their (react-bootstrap) documentation page,here is the link you can follow/go to react-bootstrap查看他们的(react-bootstrap)文档页面,这是您可以关注/转到react-bootstrap的链接

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

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