reactjsreact-routerjsxreact-functional-componentreact-lazy-load

I'm getting following error "Cannot read properties of undefined (reading 'then') at lazyInitializer at resolveLazy " while using React Lazy


This is the Error I'm getting

//This is my code where I used the react lazy in React functional component.

import React,{Suspense, lazy} from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Loader from "./components/loader"
const AboutLazy = lazy(()=>{import("./pages/aboutus")})
 <BrowserRouter>
      <HomeContext.Provider value={{ homes}}>    
            <Suspense fallback={<Loader />}>     
            <Routes>
            <Route path="/aboutus" element={<AboutLazy />} /> 
            </Routes>
           </ Suspense > 
           </>
      </HomeContext.Provider>
    </BrowserRouter>
    </>

I'm expecting to lazy load the webPage using react lazy


Solution

  • Modify your import command

    const AboutLazy = lazy(()=>{import("./pages/aboutus")})
    

    to remove the {} parenthesis since you are trying to import a component and not return anything

    replace with this one:

    lazy(()=> import("./pages/aboutus"))