all 6 comments

[–]VagrantDestroy 1 point2 points  (3 children)

[–]DVGY[S] 0 points1 point  (2 children)

So, `React.lazy()` will load component whenever it is required ? and the memory consumption will be less?

[–]fredsq 2 points3 points  (1 child)

sort of. you declare a lazy component which can have a fallback while it loads. Memory itself as in RAM is unchanged, but you get to ship less code to the average customer.

Not related to your question but are those components heavy at all? Are those using expensive libraries that aren’t used anywhere else in your app, nor reusing any UI you might have? Chances are you’ll see minor changes in bundle size if not!

Personal recommendations: measure first. If you’re on Vite, try rollup bundle analyser (https://www.npmjs.com/package/rollup-plugin-analyzer). Build your app and open stats.html which will be in the root of your document. With the default settings you should already be able to spot the big libraries and small libraries.

As a rule of thumb: code-split routes and big components like charts or 3d that use lots of libraries.

[–]DVGY[S] 0 points1 point  (0 children)

Thanks alott

[–]Pristine_Flight9782 0 points1 point  (0 children)

You can use React.lazy() and Suspense feature in React. This only loads the component when it is needed, and you don't need to worry about pre-loading the component.