When building a React application, we may have multiple components such as Home, About, Contact, Dashboard, and Profile.
Normally, React loads the imported components as part of the application's JavaScript bundle.
But what if a user only visits the Home page? Do we really need to load the About and Contact pages immediately?
This is where Lazy Loading comes into the picture.
1. What is Lazy Loading?
Lazy Loading is a technique in React that allows us to load a component only when it is needed instead of loading everything at the beginning.
It helps reduce the initial JavaScript bundle size and can improve the initial loading performance of an application.
2. Why do we need Lazy Loading?
Imagine an application has 10 different pages.
Without Lazy Loading:
- Components are included in the initial bundle (unless other code-splitting mechanisms are configured).
- The browser may need to download more JavaScript initially.
- Initial loading can become slower as the application grows.
With Lazy Loading:
- Components can be loaded when required.
- The initial bundle can become smaller.
- Unnecessary JavaScript does not need to be downloaded immediately.
3. How to implement Lazy Loading?
React provides a built-in function called lazy().
We use lazy() to dynamically import a component.
Syntax:
import { lazy } from 'react';
const About = lazy(() => import('./About'));
Here:
-
lazy()tells React to load the component when it is first rendered. -
import()dynamically imports the component. - The component must be exported as a default export.
4. What is Suspense?
When a lazy-loaded component is being downloaded, React needs something to display temporarily.
For this purpose, React provides a component called Suspense.
We use the fallback prop to display temporary content while the component is loading.
Example:
import React, { lazy, Suspense } from 'react';
const About = lazy(() => import('./About'));
function App() {
return (
<div>
<h1>Home Page</h1>
<Suspense fallback={<h2>Loading About...</h2>}>
<About />
</Suspense>
</div>
);
}
export default App;
In this example:
- React starts loading the About component when it is rendered.
- While the component is loading, Suspense displays
Loading About.... - Once the component is loaded, React displays the About component.
5. Lazy Loading with React Router
Lazy Loading is especially useful when working with multiple pages.
import React, { lazy, Suspense } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
const Home = lazy(() => import('./Home'));
const About = lazy(() => import('./About'));
function App() {
return (
<BrowserRouter>
<Suspense fallback={<h2>Loading Page...</h2>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Suspense>
</BrowserRouter>
);
}
export default App;
Now, the Home and About pages can be loaded separately when their routes are rendered.
6. Can we use Suspense without Lazy Loading?
Yes. Suspense is not limited to Lazy Loading.
It can also be used with other Suspense-enabled features, such as certain data-fetching libraries.
However, simply wrapping a normal component inside Suspense does not automatically make that component load lazily.
7. Important Points to Remember
-
lazy()is used to dynamically load React components. -
import()is used for dynamic imports. -
Suspensedisplays fallback content while a lazy component is loading. -
fallbackaccepts React elements, such as text, components, or loaders. - Lazy-loaded components should use default exports.
- Lazy Loading helps implement code splitting.
Conclusion
Lazy Loading is an important performance optimization technique in React.
Instead of loading every component at the beginning, we can load components only when they are required.
Top comments (0)