Source:  Twitter logo

I'm trying to get a dropdown working inside a form for one of my React components. 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>
//<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:

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:

npm i --save bootstrap jquery popper.js

add it to 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

40 users liked answer #0dislike answer #040
Alexey Nikonov profile pic
Alexey Nikonov

It's because you've added HTML and CSS but not the js for it. 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"));
24 users liked answer #1dislike answer #124
teimurjan profile pic
teimurjan

In App.js Import like this

import "../node_modules/bootstrap/dist/css/bootstrap.min.css";

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

3 users liked answer #2dislike answer #23
Khayal Sharif profile pic
Khayal Sharif

after a long searching and tweaking I got the answer here

you just have to add a css to resolve this issue. 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

  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.
2 users liked answer #3dislike answer #32
M K Garwa profile pic
M K Garwa

For me this issue was fixed by using Bootstrap 4.5 instead of 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.

import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.css';
2 users liked answer #4dislike answer #42
Doh09 profile pic
Doh09

Add bootstrap as a module -

npm i --save bootstrap

From my experience, please add this line to your index.js and try again -

import 'bootstrap';

Let me know how it goes.

1 users liked answer #5dislike answer #51
Vikram Gulia profile pic
Vikram Gulia
import 'bootstrap/dist/js/bootstrap'

add this to app.js or index.js file

1 users liked answer #6dislike answer #61
ABHISHEK SHAW profile pic
ABHISHEK SHAW

To solve this issue need to pass rootCloseEvent, for example

 
    <DropdownButton                          
    title="Download"
    rootCloseEvent="mousedown"
  >
    <MenuItem
      onClick={}
    >
      PDF
    </MenuItem>
    <MenuItem
      onClick={}
    >
      CSV
</MenuItem>
<DropdownButton/>
0 users liked answer #7dislike answer #70
Krishnendu Pal profile pic
Krishnendu Pal

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).