Bootstrap modal doesn't work in React Typescript app but works on fiddle


Zameer ansari

According to my current research:

Here is the debug version https://preprod-ansaar.web.app/

I have implemented a bootstrap modal in my program app.tsx:

import React from 'react';
const app: React.FC = () => {
  return (
    <div>
      <button id="btn1">Show Modal</button>
      <br />
      <button data-onclick="alert('Button Clicked')">Another Button</button>


      <div className="modal fade" id="myModal" data-tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div className="modal-dialog" role="document">
          <div className="modal-content">
            <div className="modal-header">
              <button className="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 className="modal-title" id="myModalLabel">Modal Title</h4>
            </div>
            <div className="modal-body">
              Modal Body
      </div>
            <div className="modal-footer">
              <button className="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

export default app;

Mine index.cssare:

#myModal {
  position: relative;
}

.modal-dialog {
  position: fixed;
  width: 100%;
  margin: 0;
  padding: 10px;
}

and in index.htmlI have:

  <script>
    $('#btn1').click(function () {
      // reset modal if it isn't visible
      if (!($('.modal.in').length)) {
        $('.modal-dialog').css({
          top: 0,
          left: 0
        });
      }
      $('#myModal').modal({
        backdrop: false,
        show: true
      });

      $('.modal-dialog').draggable({
        handle: ".modal-header"
      });
    });
  </script>

The same code generates a draggable modal while keeping the background available on the fiddle

Also doesn't work in my React app, can anyone guide me on this?

Zameer ansari

As @nithin suggested in the comments , the click handler is registered before it's ready , which makes it redundant. Try moving the click handler to .DOMcomponentDidMount

componentDidMount() {    
    //modal logic goes here    
}

The fiddle works DOMbecause it 's initialized before the script executes (since you're not using react there)

Related


Bootstrap Dropdown doesn't work locally but on Fiddle

lance I am building a MVC 5webapp and trying to create a split dropdown button. My code works fine on Fiddle: <div class="btn-group pull-right"> <button id="ClearRules" type="button" class="btn btn-xs btn-default" title="Clear all of the rules"><span clas

Bootstrap Dropdown doesn't work locally but on Fiddle

lance I am building a MVC 5webapp and trying to create a split dropdown button. My code works fine on Fiddle: <div class="btn-group pull-right"> <button id="ClearRules" type="button" class="btn btn-xs btn-default" title="Clear all of the rules"><span clas

Bootstrap Dropdown doesn't work locally but on Fiddle

lance I am building a MVC 5webapp and trying to create a split dropdown button. My code works fine on Fiddle: <div class="btn-group pull-right"> <button id="ClearRules" type="button" class="btn btn-xs btn-default" title="Clear all of the rules"><span clas

Bootstrap Dropdown doesn't work locally but on Fiddle

lance I am building a MVC 5webapp and trying to create a split dropdown button. My code works fine on Fiddle: <div class="btn-group pull-right"> <button id="ClearRules" type="button" class="btn btn-xs btn-default" title="Clear all of the rules"><span clas

Bootstrap Dropdown doesn't work locally but on Fiddle

lance I am building a MVC 5webapp and trying to create a split dropdown button. My code works fine on Fiddle: <div class="btn-group pull-right"> <button id="ClearRules" type="button" class="btn btn-xs btn-default" title="Clear all of the rules"><span clas

Date picker (.js) doesn't work in HTML editor but works in fiddle

Gladwin James This is the working format in the fiddle , below is the code I use in the demo site, I created a new folder name js and put the datepicker.js in it. So I linked the following in my html. <script type="text/javascript" src="js/datepicker.js"></scr

Date picker (.js) doesn't work in HTML editor but works in fiddle

Gladwin James This is the working format in the fiddle , below is the code I use in the demo site, I created a new folder name js and put the datepicker.js in it. So I linked the following in my html. <script type="text/javascript" src="js/datepicker.js"></scr

Date picker (.js) doesn't work in HTML editor but works in fiddle

Gladwin James This is the working format in the fiddle , below is the code I use in the demo site, I created a new folder name js and put the datepicker.js in it. So I linked the following in my html. <script type="text/javascript" src="js/datepicker.js"></scr

Date picker (.js) doesn't work in HTML editor but works in fiddle

Gladwin James This is the working format in the fiddle , below is the code I use in the demo site, I created a new folder name js and put the datepicker.js in it. So I linked the following in my html. <script type="text/javascript" src="js/datepicker.js"></scr

Date picker (.js) doesn't work in HTML editor but works in fiddle

Gladwin James This is the working format in the fiddle , below is the code I use in the demo site, I created a new folder name js and put the datepicker.js in it. So I linked the following in my html. <script type="text/javascript" src="js/datepicker.js"></scr

Bootstrap modal doesn't work

curt.jpeg I have a frustrating problem. My modal cannot be opened. I have a "localhost:8888/book/#rollrModal" white box in the bottom left of my browser, and that's it. and no console warnings etc. Here is the JSFiddle with the relevant code: https://jsfiddle.

Bootstrap modal doesn't work

curt.jpeg I have a frustrating problem. My modal cannot be opened. I have a "localhost:8888/book/#rollrModal" white box in the bottom left of my browser, and that's it. and no console warnings etc. Here is the JSFiddle with the relevant code: https://jsfiddle.

Modal bootstrap doesn't work

Zor I put the live demo into a new page : <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <div class="modal fade"> <div class="modal-dialog"> <div class="modal-content">

Bootstrap modal doesn't work

Timo I'm trying to get a modal from bootstrap, but for some reason it's showing like this: This is the default code I use from the bootstrap documentation: <!-- Button trigger modal --> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" d

Bootstrap modal doesn't work

curt.jpeg I have a frustrating problem. My modal cannot be opened. I have a "localhost:8888/book/#rollrModal" white box in the bottom left of my browser, and that's it. and no console warnings etc. Here is the JSFiddle with the relevant code: https://jsfiddle.

Bootstrap modal doesn't work

curt.jpeg I have a frustrating problem. My modal cannot be opened. I have a "localhost:8888/book/#rollrModal" white box in the bottom left of my browser, and that's it. and no console warnings etc. Here is the JSFiddle with the relevant code: https://jsfiddle.

Bootstrap modal doesn't work?

fishermen My modal div is: <div class="modal" id="showmodal"><!-- content here --> </div> Now , after the DOM is attached with the anchor, I call it with data-toggle='modal' : <a class="c" data-toggle="modal" data-target="#showmodal">click </a> and its work.

Modal bootstrap doesn't work

Zor I put the live demo into a new page : <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> Launch demo modal </button> <div class="modal fade"> <div class="modal-dialog"> <div class="modal-content">

Bootstrap modal doesn't work

Timo I'm trying to get a modal from bootstrap, but for some reason it's showing like this: This is the default code I use from the bootstrap documentation: <!-- Button trigger modal --> <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" d

Bootstrap modal doesn't work with Bootstrap carousel

learner I have a bootstrap template that opens by clicking the link. But this doesn't work for bootstrap carousels on the page. I tried putting the carousel HTML on top of the modal HTML and vice versa but nothing helped :( The modal looks like this - But actu

Bootstrap modal doesn't work with Bootstrap carousel

learner I have a bootstrap template that opens by clicking the link. But this doesn't work for bootstrap carousels on the page. I've tried putting the carousel HTML on top of the modal HTML and vice versa, but nothing helped :( The modal looks like this - But

Bootstrap modal doesn't work with Bootstrap carousel

learner I have a bootstrap template that opens by clicking the link. But this doesn't work for bootstrap carousels on the page. I've tried putting the carousel HTML on top of the modal HTML and vice versa, but nothing helped :( The modal looks like this - But