简体   繁体   English

将 bootstrap 5 组件导入 create-react-app

[英]Import bootstrap 5 component into create-react-app

Hi and thanks for the help, I'm using bootstrap 5 to style an app in react, but I'm having trouble including the bootstrap component js.,感谢您的帮助,我正在使用 bootstrap 5 来设置应用程序的样式,但是我在包含 bootstrap 组件 js 时遇到了麻烦。 Here I share the code of how I do the import.在这里,我分享我如何进行导入的代码。

import "bootstrap/dist/css/bootstrap.css";
import { Dropdown } from "bootstrap";

function App() {

  return (
      <div>
          <div className="dropdown">
            <a 
                className="btn btn-secondary dropdown-toggle" 
                href="#/" 
                role="button" 
                id="dropdownMenuLink" 
                data-bs-toggle="dropdown" 
                aria-expanded="false">
                   Dropdown link
           </a>

          <ul className="dropdown-menu" aria-labelledby="dropdownMenuLink">
             <li><a className="dropdown-item" href="#/">Action</a></li>
             <li><a className="dropdown-item" href="#/">Another action</a></li>
             <li><a className="dropdown-item" href="#/">Something else here</a></li>
         </ul>
       </div>
    </div>
  );
}

export default App;

When executing the code with npm start the app works fine and I have no problem with the bootstrap component the Dropdown displays.使用npm start应用程序工作正常,并且下拉显示的引导组件没有问题。 The problem is that in the console I receive a warning is the following:问题是在控制台中我收到以下警告:

WARNING in src\component\Sidebar.jsx Line 5:10: 'Dropdown' is defined but never used no-unused-vars src\component\Sidebar.jsx 第 5:10 行中的警告:“Dropdown”已定义但从未使用 no-unused-vars

webpack 5.65.0 compiled with 1 warning in 696 ms webpack 5.65.0 在 696 毫秒内编译时出现 1 个警告

I understand that I have to define Dropdown somewhere, but I don't know where and how.我知道我必须在某个地方定义 Dropdown,但我不知道在哪里以及如何。 I appreciate the help in advance.我提前感谢您的帮助。 The solution to this question is a way to eliminate abstinence using good practice.这个问题的解决方案是通过良好的实践来消除禁欲。 Well, a simple solution is adding: // eslint-disable-next-line and esLint would not show it, but this is not a definitive solution.好吧,一个简单的解决方案是添加: // eslint-disable-next-line and esLint不会显示它,但这不是一个确定的解决方案。

  • Using react bootstrap is not a solution使用反应引导不是解决方案

Update更新

https://codesandbox.io/s/loving-platform-q6sqy?file=/src/App.js example in codesandbox https://codesandbox.io/s/loving-platform-q6sqy?file=/src/App.js在codesandbox中的例子

Importing {Dropdown} means you are going to use it as a tag like this: <Dropdown> .导入 {Dropdown} 意味着您将使用它作为这样的标签: <Dropdown> If you want to use the dropdown class just like you are doing I guess just importing bootstrap would be enough.如果您想像您一样使用下拉菜单 class 我想只导入引导程序就足够了。 So you don't need the second import line.所以你不需要第二条导入线。

This is correct, you should remove the second import as you import Dropdown class from the bootstrap library, not a React component, so this second import will not work.这是正确的,您应该在从引导库而不是 React 组件导入 Dropdown class 时删除第二次导入,因此第二次导入将不起作用。 If you would like to use it as a React component, you need to install react-bootstrap.如果您想将其用作 React 组件,则需要安装 react-bootstrap。

Since you are using only bootstrap classes you are good to go, this first import is a correct way of importing bootstrap CSS into the project.由于您仅使用引导程序类,因此您对 go 很好,因此第一次导入是将引导程序 CSS 导入项目的正确方法。

I have managed to import the js of the bootstrap 5 component, with no errors.我已经成功地导入了 bootstrap 5 组件的 js,没有任何错误。 Here the code这里的代码

import "bootstrap/dist/css/bootstrap.css";
import "bootstrap/js/dist/dropdown.js";

function App() {
  return (
    <div>
      <div className="dropdown">
        <a
          className="btn btn-secondary dropdown-toggle"
          href="/"
          role="button"
          id="dropdownMenuLink"
          data-bs-toggle="dropdown"
          aria-expanded="false"
        >
          Dropdown link
        </a>

        <ul className="dropdown-menu" aria-labelledby="dropdownMenuLink">
          <li>
            <a className="dropdown-item" href="/">
              Action
            </a>
          </li>
          <li>
            <a className="dropdown-item" href="/">
              Another action
            </a>
          </li>
          <li>
            <a className="dropdown-item" href="/">
              Something else here
            </a>
          </li>
        </ul>
      </div>
    </div>
  );
}

export default App;

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

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