【问题标题】:How to use the react-bootstrap modal如何使用 react-bootstrap 模式
【发布时间】:2019-11-03 15:54:44
【问题描述】:

我有一个名为 Navbar 的组件。当我单击导航栏组件中的记笔记按钮时,我希望它使用 react-bootstrap 显示模式。我该怎么办。这是我的代码

class Navbar extends Component {    
    render(){
        return (
            <React.Fragment>
                <nav style={navStyle} 
                    className="navbar navbar-expand-md">
                    <p style={noteStyle}>Notes</p>
                    <button 
                        style={btnStyle}
                        className="btn btn-light">
                        Take Note
                    </button>
                </nav>
            </React.Fragment>
        )
    }
};

【问题讨论】:

    标签: reactjs react-bootstrap


    【解决方案1】:

    这是一个快速的demo。希望对您有所帮助。

    import React, { useState } from "react";
    import "bootstrap/dist/css/bootstrap.min.css";
    import "./styles.css";
    import { Button, Modal } from "react-bootstrap";
    
    const styles = {
      navStyle: { background: "red" },
      noteStyle: { color: "yellow" },
      btnStyle: { background: "blue" }
    };
    
    const App = () => {
      const [show, setShow] = useState(false);
    
      const handleClose = () => setShow(false);
      const handleShow = () => setShow(true);
    
      return (
        <>
          <nav style={styles.navStyle} className="navbar navbar-expand-md">
            <p style={styles.noteStyle}>Notes</p>
    
            <Button style={styles.btnStyle} variant="primary" onClick={handleShow}>
              Take Note
            </Button>
          </nav>
    
          <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>
        </>
      );
    };
    

    【讨论】:

      【解决方案2】:

      基本上,只要您想点击导航栏,就需要触发模式。您可以在 react-bootstrap 组件库上轻松找到参考。如何显示和隐藏模式(即使用状态标志)。我添加了代码以供参考。我建议您自己进行调整以更快地学习。

      import React, { useState } from "react";
      import ReactDOM from "react-dom";
      import {
        Navbar,
        NavbarBrand,
        Nav,
        Button,
        Modal,
        ModalHeader,
        ModalBody,
        ModalFooter
      } from "reactstrap";
      
      import "./styles.css";
      
      function App() {
        const [modal, setModal] = useState(false);
        const toggle = () => setModal(!modal); //Set hide or show modal
      
        return (
          <div className="App">
            <Navbar color="light" light expand="md">
              <NavbarBrand href="/">reactstrap</NavbarBrand>
              <Nav className="ml-auto" navbar>
                <Button onClick={toggle}>Take Note</Button> // Show modal
              </Nav>
            </Navbar>
          //Modal
            <Modal isOpen={modal} toggle={toggle}>
              <ModalHeader toggle={toggle}>Create Note</ModalHeader>
              <ModalBody>Your Note</ModalBody>
              <ModalFooter>
                <Button color="primary" onClick={toggle}>
                  Do Something More
                </Button>{" "}
                <Button color="secondary" onClick={toggle}>
                  Cancel
                </Button>
              </ModalFooter>
            </Modal>
          </div>
        );
      }
      
      const rootElement = document.getElementById("root");
      ReactDOM.render(<App />, rootElement);
      
      
      

      现场演示:https://codesandbox.io/s/pensive-leaf-vkrqf

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        相关资源
        最近更新 更多