Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Next.js and React · Troubleshooting walkthrough

A Bootstrap 5 dropdown in a React app does not open

Short answer

A Bootstrap dropdown that will not open in React has one of three causes: only the CSS was imported, so nothing handles the click; the toggle uses Bootstrap 4's data-toggle instead of data-bs-toggle; or a plain bootstrap.js build runs without Popper. Import Bootstrap's JavaScript once in the entry file and use data-bs-toggle. Applies to Bootstrap 5.3.

  • Applies to Bootstrap 5.3.9, @popperjs/core 2.11.8, React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

In a React app the dropdown fails silently for one of three reasons. Bootstrap's JavaScript was never loaded, so only the stylesheet exists. The markup uses Bootstrap 4's data-toggle instead of Bootstrap 5's data-bs-toggle. Or a plain bootstrap.js build runs without Popper, which the dropdown needs to position its menu.

A short search plus one click with the console open tells you which one you have. The notes below are for Bootstrap 5.3.

The text you see

  • Clicking the dropdown toggle does nothing and the console stays empty
  • TypeError: Bootstrap's dropdowns require Popper (https://popper.js.org/docs/v2/)
  • TypeError: Bootstrap's dropdowns require Popper (https://github.com/floating-ui/popper-docs/blob/main/docs/v2/index.md)

Stage 1

Find out what is loaded

Check where Bootstrap's JavaScript comes from, which attribute the toggle uses, and which versions are installed. Then click the toggle with the browser console open. Nothing in the console means the plugin never ran. A TypeError mentioning Popper points to the third reason.

Three quick checks from the project root

grep -rnE "import .*bootstrap|bootstrap(\.bundle)?(\.min)?\.js" src/ index.html
grep -rn 'data-toggle="dropdown"' src/
npm ls bootstrap @popperjs/core

Stage 2

Only the CSS is imported

The stylesheet draws the menu, but opening it takes JavaScript. Bootstrap's dropdown plugin registers a click handler on the document for elements with data-bs-toggle="dropdown", so once the script has run, it also serves toggles that React renders later. With no script there is no handler.

Install Bootstrap with Popper and import its JavaScript once, in the entry file beside the CSS.

Entry file

npm install bootstrap @popperjs/core

// src/main.jsx
import 'bootstrap/dist/css/bootstrap.min.css';
import * as bootstrap from 'bootstrap';

Stage 3

Bootstrap 4 attributes in a Bootstrap 5 app

Bootstrap 5 moved every data attribute of its plugins into a bs namespace, and the migration guide lists the change. A toggle with data-toggle is ignored: the button looks right and nothing opens. Snippets copied from older tutorials are the usual source.

JSX with the Bootstrap 5 attribute

<div className="dropdown">
  <button className="btn btn-secondary dropdown-toggle" type="button"
          data-bs-toggle="dropdown" aria-expanded="false">
    Options
  </button>
  <ul className="dropdown-menu">
    <li><a className="dropdown-item" href="#">Action</a></li>
  </ul>
</div>

Rename related attributes too, such as data-target and data-offset, to their data-bs-* form.

Stage 4

Plain bootstrap.js without Popper

The dropdown places its menu with Popper and throws Bootstrap's dropdowns require Popper when it is missing. This happens when dist/js/bootstrap.js or bootstrap.min.js is loaded directly, because those builds do not include Popper. The bundle build includes it, and importing from the package entry, as above, pulls Popper in for you. Version 5.3.9 only changed the link at the end of the message.

// switch the plain build for the bundle
import 'bootstrap/dist/js/bootstrap.bundle.min.js';
// script-tag setups, in index.html:
<script src="/vendor/bootstrap.bundle.min.js"></script>

Stage 5

Click the toggle and read the show class

  1. Click the toggle: the menu opens and gains the show class.
  2. The console shows no TypeError.
  3. Search for leftover data-toggle or data-target and expect none.
document.querySelector('.dropdown-menu').classList.contains('show')

Stage 6

If it opens and then misbehaves

If the menu opens but closes or sticks after React re-renders, check that your own code is not also adding or removing the show class. Give the open state one owner: Bootstrap or React, not both.

If you only keep one thing

  • No JavaScript import, a data-toggle, or no Popper: one of those three.
  • Import bootstrap once in the entry file and use data-bs-toggle.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.