如何使用react-bootstrap模式

时间:2019-11-03 06:31:59

标签: reactjs react-bootstrap

我有一个名为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>
        )
    }
};

2 个答案:

答案 0 :(得分:0)

这里是一个快速的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>
    </>
  );
};

答案 1 :(得分:0)

基本上,每当您要单击导航栏时,都需要触发模式。您可以在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