0

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?

5
  • Did you import lazy from React using import { lazy } from 'react'?
    – Yaniek
    Commented Mar 1 at 6:57
  • Also to load bootstrap before JSX you have to remove the defer attribute when you import it in index.html.
    – Yaniek
    Commented Mar 1 at 7:00
  • Yes i did it and also removed defer but still not working Commented Mar 1 at 12:24
  • What do you mean by 'not working'?
    – Yaniek
    Commented Mar 1 at 12:38
  • Layout of the page shifting upward and left side after applying lazy loading. I have even preloaded it's css in index.html but still facing same issue. Commented Mar 4 at 7:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.