I am using React-Bootstrap in my React app, and it was working fine until I implemented lazy loading for my components using React.lazy() and Suspense. After enabling lazy loading, React-Bootstrap components (e.g., Container, Image, Modal) are not working properly.
What I have tried:
Ensuring Bootstrap is loaded
Imported Bootstrap CSS and JS in App.jsx:
import "bootstrap/dist/css/bootstrap.min.css";
import "bootstrap/dist/js/bootstrap.bundle.min.js";
Added Bootstrap CDN links in index.html:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
Testing without lazy loading
When I remove lazy loading (React.lazy()), React-Bootstrap components work fine.
Loading Bootstrap before rendering JSX
Some AI-generated suggestions said that Bootstrap should load before JSX, but I couldn't find an effective way to enforce this.
My Implementation:
Here’s how I am using lazy loading:
const LazyComponent = React.lazy(() => import("./LazyComponent"));
function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
}
What could be causing this issue, and how can I ensure React-Bootstrap works correctly with lazy-loaded components?
import { lazy } from 'react'
?bootstrap
before JSX you have to remove thedefer
attribute when you import it in index.html.