Pulsara Sandeepa
Pulsara Sandeepa

Reputation: 945

Bootstrap Dropdown is not working in React

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

Upvotes: 8

Views: 9482

Answers (4)

Krishnendu Pal
Krishnendu Pal

Reputation: 1

To solve this issue need to pass rootCloseEvent, for example

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

Upvotes: 0

kevinskywalker
kevinskywalker

Reputation: 1

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.

Upvotes: 0

Yogesh Yadav
Yogesh Yadav

Reputation: 863

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>
  );
}

Upvotes: 1

Nisharg Shah
Nisharg Shah

Reputation: 19532

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

Upvotes: 11

Related Questions