Bootstrap modal doesn't work in React Typescript app but works on fiddle
Zameer ansari
According to my current research:
Tried removing the fade class , no difference
Assets are loaded sequentially, so there is no difference
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">×</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.css
are:
#myModal {
position: relative;
}
.modal-dialog {
position: fixed;
width: 100%;
margin: 0;
padding: 10px;
}
and in index.html
I 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 .DOM
componentDidMount
componentDidMount() {
//modal logic goes here
}
The fiddle works DOM
because it 's initialized before the script executes (since you're not using react there)