question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug with Bootstrap Collapse in combination with Bootstrap Toast - ReactJS

See original GitHub issue

Hi,

I think I may have found a bug between the Bootstrap v5.1.3 collapse feature and Toast-Messages in ReactJS

Setup to replicate the error:

Create a default react project with:

  • npx create-react-app collapse-toast-test
  • Import boostrap with npm i bootstrap or yarn i bootstrap
  • In index.js import bootstrap:
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap.bundle.js';
  1. Use buttons from example: https://getbootstrap.com/docs/5.1/components/collapse/ and paste the code into app.js
  2. In app.js import Toast (and make use of it if you want)
import { Toast } from "bootstrap";

Result:

Pressing the buttons only shows the hidden elements. Hide afterwards is not working. So the toggle-feature is bugged.

Used versions:

React-Version: 18.0.0 Bootstrap-Version: 5.1.3

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
victory-sokolovcommented, Sep 21, 2022

Another workaround is to import Toast directly from toast.js

import Toast from 'bootstrap/js/dist/toast.js';

1reaction
RyanBerlinercommented, May 17, 2022

The bug you are seeing has nothing to do with React, and almost nothing to do with bootstrap either. Rather, it’s your improper import of bootstraps javascript. You are explicitly importing bootstrap’s umd file (bundled with popper… but that irrelevant here), then importing the Toast class from bootstraps default path… which in the case of the bundler in create-react-app is going to be the module path, ie bootstraps esm file. This effectively loads certain parts of bootstraps code base twice and you are seeing the effects of this.

So instead of

import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/js/bootstrap.bundle.js';

// and later

import { Toast } from "bootstrap";

You’ll want

import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap';

// and later

import { Toast } from "bootstrap";

The import 'bootstrap'; is equivalent to import 'bootstrap/dist/js/bootstrap.esm'; in this case - though I would not recommend the long hand import. Use the default and let your bundler decide what kind of files it wants.

Note: You will need to make sure popper is installed as well - as you would no longer be loading the “bootstrap bundle” file that included everything in 1 file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Bootstrap Collapse is not working - Stack Overflow
Started using React-Bootstrap today and I want to make a collapsable card. Using Collapse component but it's not working. Any tips? My code:...
Read more >
JavaScript · Bootstrap v5.2
Both Bootstrap and the framework may attempt to mutate the same DOM element, resulting in bugs like dropdowns that are stuck in the...
Read more >
react-bootstrap | Yarn - Package Manager
Bootstrap compatibility​​ React-Bootstrap is compatible with various versions of Bootstrap. As such, you need to ensure you are using the correct combination of ......
Read more >
Toast - dbc docs - Dash Bootstrap Components
Icons and dismissing ... You can use the icon argument to add a contextually coloured icon to the header. You can also add...
Read more >
Bootstrap Reactstrap Collapse is not working with list of ...
import React, { useState } from "react"; import { Card, CardText } from "reactstrap"; function Story(props) { console.log(props) return ( <Card ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found