34

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?

FlameDra
  • 1,807
  • 7
  • 30
  • 47
  • 1
    i hope you already have the `bootstrap.js` on your app? – Inus Saha Jun 22 '18 at 03:12
  • Did you link bootstrap js file to your page ? Read the same question [here](https://stackoverflow.com/questions/40037657/how-to-include-bootstrap-css-and-js-in-reactjs-app) – dungmidside Jun 22 '18 at 04:03
  • I added my index.js file code into my main post, how do I add bootstrap.js to this file? – FlameDra Jun 22 '18 at 05:29

14 Answers14

41

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

Alexey Nikonov
  • 4,958
  • 5
  • 39
  • 66
  • Follow the source for a further understanding of this question: https://blog.logrocket.com/how-to-use-bootstrap-with-react-a354715d1121 – viarnes May 22 '19 at 19:31
  • 3
    Thanks, this worked with minor change, after installing bootstrap & proper.js `npm i bootstrap@4.4.1` `npm i --save bootstrap jquery popper.js` I only imported the following 2 lines into index.js: `import 'bootstrap/dist/css/bootstrap.css';` `import 'bootstrap/js/dist/dropdown';` Note: you'd need to restart the application after: `npm i --save bootstrap jquery popper.js` – Richard Mneyan Jan 05 '20 at 00:34
  • 1
    You can import just import "bootstrap/dist/js/bootstrap.bundle"; into index.js. – yasserpulido Apr 15 '22 at 05:39
25

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"));
teimurjan
  • 1,875
  • 9
  • 18
  • So I need to make a separate Dropdown component and use that in the component I want a drop down in? – FlameDra Jun 22 '18 at 05:30
  • No, it’s just for example. You need to pass onClick to the drop down component which will toggle its open state. According to this open state you will whether add “show” className or not. If you add this className your drop down will be visible otherwise it won’t. – teimurjan Jun 22 '18 at 05:45
  • That doesn't work the same as bootstrap, for instance, if you click away from the drop menu it stays open, you have to click again to close it. Bootstrap closes when you click away from the dropdown. I had the same problem and tried this solution but could not make it work the same – djack109 Jan 05 '21 at 22:29
7

In bootstrap-5 in app.js add the below files

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

Happy coding

  • I had previously added "import 'bootstrap/dist/css/bootstrap.css';" only and it was not working. It automatically started working when I included "import "bootstrap/dist/js/bootstrap.bundle.min.js";". This solved my problem. Thank you! – Alfredo Rodriguez Jan 05 '23 at 01:36
6

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";

Khayal Sharif
  • 61
  • 1
  • 2
3

If you use bootstrap ^5 you should use data-bs-* attributes like :

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:

  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';
Ali Reza
  • 61
  • 3
3
import "../node_modules/bootstrap/js/dist/dropdown.js"

Go inside the node_modules, then bootstrap, js, and dist. Inside that, you will find dropdown.js. Just import this and the dropdown will start working!

InSync
  • 4,851
  • 4
  • 8
  • 30
gaurav
  • 31
  • 1
  • it is never good to import directly from your node modules, the solution should be something else – Xiduzo May 03 '23 at 19:15
2

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.
M K Garwa
  • 495
  • 2
  • 13
2

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';
Doh09
  • 2,324
  • 1
  • 16
  • 31
2
import 'bootstrap/dist/js/bootstrap'

add this to app.js or index.js file

  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Jan 12 '22 at 16:15
1

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.

Vikram Gulia
  • 903
  • 10
  • 17
  • Module not found: Can't resolve 'jquery' in '/Users/wipe/Desktop/ironhack/lab-react-ironbeers/starter-code/node_modules/bootstrap/dist/js' – ValRob May 31 '19 at 18:30
  • bootstrap needs jquery, Please install that in your project. – Vikram Gulia Jun 04 '19 at 17:04
  • dont use jquery in your bootstrap project. – ValRob Jun 05 '19 at 12:16
  • I don't understand what do you mean. I am using bootstrap in my react project and using web pack to bundle jquery with my app and then only thing i need to do is import 'bootstrap'; – Vikram Gulia Jun 06 '19 at 14:47
  • Jquery is for the Dom, react doesnt work with the Domr, it work with the virtual Dom. [Google it](https://www.google.com/search?q=don%27t+use+jquery+in+react&spell=1&sa=X&ved=0ahUKEwjV8Mfc69biAhUbAmMBHToFC5wQBQgrKAA&biw=1366&bih=657) – ValRob Jun 07 '19 at 07:26
  • 2
    if you're using create-react-app the correct way to import bootstrap is ```import 'bootstrap/dist/css/bootstrap.css'``` – Sarah Hailey Jul 11 '19 at 14:42
1

I was missing an import

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

Now the dropdown is working for me.

Xab Ion
  • 1,105
  • 1
  • 11
  • 20
0

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

Add onClick event and put event.stopPropagation().

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Prateek Varshney Oct 31 '22 at 04:36
0

If you're using Next.js, you should include a useEffect() in _app.js like so:

export default function App({ Component, pageProps }) {
    useEffect(() => {
        require("bootstrap/dist/css/bootstrap.min.css")
        require("bootstrap/dist/js/bootstrap.bundle.min.js")
    }, [])
    return (
        <>
            <Head>
                <meta name="viewport" content="width=device-width, initial-scale=1" />
            </Head>
            <Layout>
                <Component {...pageProps} />
            </Layout>
        </>
    )
}

Otherwise, it may give you an error saying "document is not defined".