8

I include a dropdown button to React from bootstrap Dropdowns, But it is not working and shows as a normal button. Can you give me a solution for this?

<div className="dropdown">
    <button className="btn btn-secondary 
                 dropdown-toggle" 
                 type="button" 
                 id="dropdownMenuButton"
                 data-toggle="dropdown"
                 aria-haspopup="true" 
                 aria-expanded="false">
                 Dropdown button
    </button>
            <div className="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a className="dropdown-item" href="#">Action</a>
                <a className="dropdown-item" href="#">Another action</a>
                <a className="dropdown-item" href="#">Something else here</a>
            </div>
</div>

The output is a normal button like this. enter image description here

4
  • Are you using react-bootstrap ? Commented Apr 19, 2020 at 14:01
  • @AjinKabeer I am not using react-bootstrap, I used "npm install bootstrap --save" this only. Commented Apr 19, 2020 at 14:09
  • Are you using create-react-app? Are other components being styled correctly by bootstrap or is it just dropdowns that it doesn't style? Commented Apr 19, 2020 at 14:11
  • Yes I am using create-react-app and others are styled correctly, only the dropdowns are not working Commented Apr 19, 2020 at 14:20

4 Answers 4

11

Dropdowns are not working without popper.js and jquery.js.

So please install popper.js and jquery.js in your system using npm install popper.js jquery --save and don't forget to include it.

With CDN

https://stackblitz.com/edit/create-react-class-m3qfxu?file=index.html

With NPM

https://stackblitz.com/edit/create-react-class-xvsthx?file=index.js

Sign up to request clarification or add additional context in comments.

3 Comments

Can you tell me how to import popper.js?
I give both examples, how to import it
@PulsaraSandeepa first you need to install npm i --save bootstrap jquery popper.js then import Popper from 'popper.js'; in js file
1

If someone doesn't want to install other dependencies, they can make it work using react useState hook.

import React, { useState } from 'react';

export default function DropCard() {
  const [dropdown, setDropdown] = useState(false);
  const toggleOpen = () => setDropdown(!dropdown);

 return (
    <div className="dropdown">
        <button onClick={toggleOpen}>
          Dropdown
        </button>
        <div
          className={`dropdown-menu ${dropdown ? 'show' : ''}`}
          aria-labelledby="dropdownMenuButton"
          >
            <a className="dropdown-item" href="#">
              Delete
            </a>
            <a className="dropdown-item" href="#">
              Pin to your Profile
            </a>
         </div>
     </div>
  );
}

Comments

0

Make sure you have the bootstrap js imported correctly

npm install --save bootstrap then import "bootstrap/dist/js/bootstrap.min.js";

This worked for me.

1 Comment

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.
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/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.