简体   繁体   English

如何打开和关闭反应引导模式?

[英]How to open & close react-bootstrap modal?

I'm new to react and I'm working with react-bootstrap.我是新手,我正在使用 react-bootstrap。 I have a perfectly working react app with 2 components and I'm using function components.我有一个完美工作的反应应用程序,它有 2 个组件,我正在使用 function 组件。 Here is the code.这是代码。

App.js应用程序.js

function App() {
  const [modalShow, setModalShow] = useState(false);

  return (
    <>
      <Button variant="primary" onClick={() => setModalShow(true)}>
        Launch vertically centered modal
      </Button>

      <MyVerticallyCenteredModal
        show={modalShow}
        onHide={() => setModalShow(false)}
      />
    </>
  );
}

MyVerticallyCenteredModal.js MyVerticallyCenteredModal.js

export default function MyVerticallyCenteredModal(props) {
    return (
        <Modal
            {...props}
            size="lg"
            aria-labelledby="contained-modal-title-vcenter"
            centered
        >
            <Modal.Header closeButton>
                <Modal.Title id="contained-modal-title-vcenter">
                    Modal heading
            </Modal.Title>
            </Modal.Header>
            <Modal.Body>
                <h4>Centered Modal</h4>
                <p>
                    Cras mattis consectetur purus sit amet fermentum. Cras justo odio,
                    dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac
                    consectetur ac, vestibulum at eros.
            </p>
            </Modal.Body>
            <Modal.Footer>
                <Button onClick={props.onHide}>Close</Button>
            </Modal.Footer>
        </Modal>
    );
}

At this stage, my code is working fine.在这个阶段,我的代码运行良好。 But I need to make a few changes.但我需要做一些改变。 I need to remove show and onHide props from MyVerticallyCenteredModal component and access props from Modal component.我需要从MyVerticallyCenteredModal组件中删除 show 和 onHide 道具,并从Modal组件中访问道具。 Here is what I did,这是我所做的,

App.js应用程序.js

<MyVerticallyCenteredModal />

MyVerticallyCenteredModal.js MyVerticallyCenteredModal.js

<Modal
  show={modalShow}
  onHide={() => setModalShow(false)}
  size="lg"
  aria-labelledby="contained-modal-title-vcenter"
  centered
>
...
</Modal>

Now I'm getting a compile error,现在我得到一个编译错误,

'modalShow' is not defined
'setModalShow' is not defined

But without moving useState from App.js to MyVerticallyCenteredModal.js how can I do it?但是如果不将 useState 从 App.js 移动到 MyVerticallyCenteredModal.js 我该怎么做呢?

The onClick(); onClick(); should take an event as for controlling mouse click:应该采取一个事件来控制鼠标点击:

 onClick={(e) => {setModalShow(false)}};

Same for onHide();与 onHide(); 相同;

Note: you can also use the word 'event' instead of the letter 'e'注意:您也可以使用单词“event”而不是字母“e”

The states is controlled from the Parent Component <App/> and the button that launches the Modal is in the App component.状态由父组件<App/>控制,启动Modalbutton位于 App 组件中。

  <Button variant="primary" onClick={() => setModalShow(true)}>
    Launch vertically centered modal
  </Button>

So, it is not possible to control the Modal behaviour from the Modal itsef.因此,无法从 Modal 自身控制 Modal 行为。

But yes if you directly render your Modal in the <App/> Component itself without using a Parent Component for rendering of Modal.但是,如果您直接在<App/>组件本身中渲染您的Modal ,而不使用父组件来渲染 Modal。 Then this can be done.然后可以做到这一点。

We will need a small button inside the modal component and use state in Modal itself to control that:)我们将需要模态组件内的一个小按钮,并在模态本身中使用 state 来控制它:)

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

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