简体   繁体   English

Bootstrap Dropdown 在 React 中不起作用

[英]Bootstrap Dropdown not working in React

I'm trying to get a dropdown working inside a form for one of my React components.我正在尝试让下拉菜单在我的一个 React 组件的表单中工作。 Here is how I'm setting up the dropdown portion of the code, the following is inside a form parent tag which is inside the div tag that I'm returning.:这是我如何设置代码的下拉部分,以下是在我要返回的 div 标签内的表单父标签内:

//<div>
//<form>
//some code here

    <div className="row top-buffer">
        <div className="col">
            <div className="dropdown">
                <button 
                    className="btn btn-secondary dropdown-toggle" 
                    type="button" 
                    id="dropdownMenuButton" 
                    data-toggle="dropdown" 
                    aria-haspopup="true">
                    Dropdown
                </button>
                <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <a className="dropdown-item" href="#nogo">Item 1</a>
                    <a className="dropdown-item" href="#nogo">Item 2</a>
                    <a className="dropdown-item" href="#nogo">Item 3</a>
                </div>
            </div>
        </div>
    </div>

//some code here
//</form>
//</div>

However, when I click the Dropdown button, it does not display the dropdown menu and the items in it.但是,当我单击下拉按钮时,它不显示下拉菜单和其中的项目。

All my other bootstrap components are working fine.我的所有其他引导程序组件都工作正常。 What am I doing wrong?我究竟做错了什么?

EDIT:编辑:

I referenced bootstrap in my index.js:我在 index.js 中引用了 bootstrap:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import 'bootstrap/dist/css/bootstrap.min.css'
import './App.css'
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

Am I missing something?我错过了什么吗?

To make dropdown menu and other js stuff work in 4th Bootstrap you need just follow next steps: install with npm to dependencies:要使下拉菜单和其他 js 内容在 4th Bootstrap 中工作,您只需按照以下步骤操作:使用 npm 安装到依赖项:

npm i --save bootstrap jquery popper.js

add it to index.js将其添加到 index.js

import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap.js';
import $ from 'jquery';
import Popper from 'popper.js';

all steps are taken from here所有步骤都从这里开始

It's because you've added HTML and CSS but not the js for it.这是因为您添加了 HTML 和 CSS,但没有为它添加 js。 Your code doesn't know what to do when you click on the dropdown.当您单击下拉菜单时,您的代码不知道该怎么做。 Here is an example how you have to do that.是一个示例,您必须这样做。 And the code below:以及下面的代码:

class Dropdown extends React.Component {
  state = {
    isOpen: false
  };

  toggleOpen = () => this.setState({ isOpen: !this.state.isOpen });

  render() {
    const menuClass = `dropdown-menu${this.state.isOpen ? " show" : ""}`;
    return (
      <div className="dropdown" onClick={this.toggleOpen}>
        <button
          className="btn btn-secondary dropdown-toggle"
          type="button"
          id="dropdownMenuButton"
          data-toggle="dropdown"
          aria-haspopup="true"
        >
          Dropdown
        </button>
        <div className={menuClass} aria-labelledby="dropdownMenuButton">
          <a className="dropdown-item" href="#nogo">
            Item 1
          </a>
          <a className="dropdown-item" href="#nogo">
            Item 2
          </a>
          <a className="dropdown-item" href="#nogo">
            Item 3
          </a>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<Dropdown />, document.getElementById("root"));

In App.js Import like this在 App.js 中像这样导入

import "../node_modules/bootstrap/dist/css/bootstrap.min.css";导入“../node_modules/bootstrap/dist/css/bootstrap.min.css”;

import "../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js";导入“../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js”;

after a long searching and tweaking I got the answer here经过长时间的搜索和调整,我在这里得到了答案

you just have to add a css to resolve this issue.您只需添加一个 CSS 即可解决此问题。 when you click on dropdown button, it adds a class "show" to that element and as per bootstrap that much is sufficient to make a dropdown button work.当您单击下拉按钮时,它会向该元素添加一个“显示”类,并且根据引导程序,足以使下拉按钮工作。

.show>.dropdown-menu {
  display: block;
  position: absolute;
}

for me it work like charm.对我来说,它就像魅力一样。

I tries @teimurjan solution and it worked for me but with two issues我尝试了@teimurjan 解决方案,它对我有用,但有两个问题

  1. I have to manage a state even though its not needed即使不需要状态,我也必须管理状态
  2. once dropdown clicked the expanded menu does not disappear on click of body or anywhere else.单击下拉菜单后,扩展菜单不会在单击正文或其他任何位置时消失。

For me this issue was fixed by using Bootstrap 4.5 instead of Bootstrap 5.对我来说,这个问题是通过使用 Bootstrap 4.5 而不是 Bootstrap 5 解决的。

To do this with yarn write:要使用纱线执行此操作,请编写:

yarn remove bootstrap
yarn add bootstrap@^4.5

In index.js I then just import it like this.在 index.js 中,我然后像这样导入它。

import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.css';

In bootstrap-5 in app.js add the below files在 bootstrap-5 的 app.js 添加以下文件

 import "bootstrap/dist/css/bootstrap.min.css"; import "bootstrap/dist/js/bootstrap.bundle.min.js";

Happy coding快乐编码

Add bootstrap as a module -将引导程序添加为模块 -

npm i --save bootstrap

From my experience, please add this line to your index.js and try again -根据我的经验,请将此行添加到您的index.js中,然后重试 -

import 'bootstrap';

Let me know how it goes.让我知道事情的后续。

import 'bootstrap/dist/js/bootstrap'

将此添加到 app.js 或 index.js 文件

If you use bootstrap ^5 you should use data-bs- * attributes like :如果您使用引导程序 ^5 ,您应该使用data-bs- * 属性,例如:

data-toggle="dropdown" must change to data-bs-toggle="dropdown"

So change all data-* wherever you use it. 因此,无论您在哪里使用,都可以更改所有数据-*。

If you use bootstrap 4 or Less than you should use jquery and popper.js like this:如果你使用bootstrap 4或小于你应该使用 jquery 和 popper.js 像这样:

    import 'bootstrap';
    import 'bootstrap/dist/css/bootstrap.css';
    import 'bootstrap/dist/js/bootstrap.js';
    import $ from 'jquery';
    import Popper from 'popper.js';

add it to index.js 将其添加到 index.js
 import 'bootstrap'; import 'bootstrap/dist/css/bootstrap.css'; import 'bootstrap/dist/js/bootstrap.js'; import $ from 'jquery'; import Popper from 'popper.js';

To solve this issue need to pass rootCloseEvent , for example要解决这个问题需要通过rootCloseEvent ,例如

 <DropdownButton title="Download" rootCloseEvent="mousedown" > <MenuItem onClick={} > PDF </MenuItem> <MenuItem onClick={} > CSV </MenuItem> <DropdownButton/>

Add onClick event and put event.stopPropagation().添加 onClick 事件并放置 event.stopPropagation()。

I was missing import "../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js";我缺少导入“../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js”; Now the dropdown is working for me现在下拉菜单对我有用

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

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