首页 > 解决方案 > 如何使用 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>
        )
    }
};

标签: reactjsreact-bootstrap

解决方案


这是一个快速演示。希望能帮助到你。

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>
    </>
  );
};

推荐阅读