Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy load a module using React lazy?

Is it possible to load a heavy module using React.lazy?

I have a very heavy raw file parsed as string (around 10 mb) and I would like to lazy load it.

After trying:

const Module = React.lazy(() =>
  import("./heavyModule").then((heavyModule) => {
    return (
      <Suspense fallback={null}>
        <SomeComponent src={heavyModule.value} />
      </Suspense>
    );
  })
);

Im getting the following error:

lazy: Expected the result of a dynamic $csbImport() call. Instead received: [object Object]

Demo

like image 390
Rashomon Avatar asked Aug 12 '26 05:08

Rashomon


1 Answers

According to you the heavyModule is not a React component

I have a very heavy raw file parsed as string (around 10 mb) and I would like to lazy load it

So you cannot use React.lazy(). Instead i would recommend to import the raw file only where you require it and then render that particular component lazily.

You can try this method :

useEffect(()=> {
 import('./heavyModule').then(data=> {
  // your code
 },[])

like image 56
Gayatri Dipali Avatar answered Aug 14 '26 14:08

Gayatri Dipali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!