Answer the question
In order to leave comments, you need to log in
How do I use React.lazy? Or what are the alternatives?
Good afternoon.
It is assumed that the site will have some functionality that will be used very rarely.
This functionality is represented by React components.
I do not want to include it in the general bundle (in order to reduce the size of the bundle).
Ideally, it would be nice to do lazy loading on demand.
At the moment, there is a loader function that takes the URL where the script needs to be loaded and returns a promise.
In the right place, we call this function. And in then/catch we process the result (either we render the component, or we process the error.
In general, we have something like this:
function loadScript(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.async = true;
script.onload = resolve;
script.onerror = reject;
script.src = url;
document.body.appendChild(script);
});
}
loadScript('/js/lazy-component.js')
.then( ... )
.catch( ... );
const OtherComponent = React.lazy(() => import('./OtherComponent'));
function MyComponent() {
return (
<div>
<OtherComponent />
</div>
);
}
<OtherComponent />
, you can wrap in Suspense.
React.lazy accepts a function that should call a dynamic import: import(). It should return a Promise that resolves to a module with
the React component's default export.
Answer the question
In order to leave comments, you need to log in
The problem is that dynamic import is used here. As I understand it, its support in browsers is not very good yet. Which means it doesn't suit me.
When you already understand that one request for one bundle is much faster than doing lazy loading by making additional requests to the server.
Yes, of course, at the first load, the rendering will take a little longer, but then this file is cached. And when loading separate scripts on the pages where they are used, it only slows it down, since time is wasted for loading individual scripts. Because everything is simple. In the header, we load the styles / js only that are needed to draw the first screen of the page, then we load everything into one bundle in the footer. Now there is no 2g where the speed was 10kb / s and you would have to wait 1 minute to download the whole bundle) In fact, the ping will most likely be higher than the download speed of the bundle with all scripts, especially if you use CDN...
If you have a bundler (webpack/rollup/etc), then the dynamic import will be moved to a separate chunk and the import() call will be converted into something more acceptable for current browsers, don't ride your bike, it's not necessary. But if you really want to, then you can return the promise to the es-module object yourself, react.lazy, no matter where you got it from.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question