Steps to Implement Lazy Loading

  • Recognize the component you want to Lazy Load. These are mostly Large or complex which is not necessary for all the users when the page loads.
  • Import the `lazy()` and Suspense components from the React package
  • Use the `lazy()` function to dynamically import the component you want to lazy load:
    Note that the argument to the `lazy()` function should be a function that returns the result of the import() function. 
  • Wrap the lazy-loaded component in a `Suspense` component, which will display a fallback UI while the component is being loaded:
<Suspense fallback={<div>Loading...</div>}>
<MyComponent />
</Suspense>

Example: This example uses the Suspense component and lazy method to implement the lazyloading for specific components.

Javascript




import React from "react";
import { Suspense, lazy } from "react";
const Component1 = lazy(() => import(
    '../src/LazyContent/myComponent1'))
const Component2 = lazy(() => import(
    '../src/LazyContent/myComponent2'))
function App() {
    return (
        <>
            <h1> Lazy Load</h1>
            <Suspense fallback=
{<div>Component1 are loading please wait...</div>}>
                <Component1 />
            </Suspense>
            <Suspense fallback=
{<div>Component2 are loading please wait...</div>}>
                <Component2 />
            </Suspense>
        </>
    );
}
 
export default App;


Output:

Lazy-Loading

Lazy Loading in React and How to Implement it ?

Lazy Loading in React JS helps to optimize the performance of React applications. The data is only rendered when visited or scrolled it can be images, scripts, etc. Lazy loading helps to load the web page quickly and presents the limited content to the user that is needed for the interaction lazy loading can be more helpful in applications that have high-resolution images or data that alters the loading time of the application.

Table of Content

  • Lazy Loading in React
  • Steps to Implement Lazy Loading
  • Advantages of using Lazy loading in react applications
  • Conclusion

Similar Reads

Lazy Loading in React:

Lazy loading is a technique in React that allows you to load components, modules, or assets asynchronously, improving the loading time of your application. React provides a built-in React.lazy() method and Suspense component to achieve lazy loading....

Steps to Implement Lazy Loading:

Recognize the component you want to Lazy Load. These are mostly Large or complex which is not necessary for all the users when the page loads. Import the `lazy()` and Suspense components from the React package Use the `lazy()` function to dynamically import the component you want to lazy load:Note that the argument to the `lazy()` function should be a function that returns the result of the import() function.  Wrap the lazy-loaded component in a `Suspense` component, which will display a fallback UI while the component is being loaded:...

Explanation:

...

Advantages of using Lazy loading in react applications:

Lazy Loading in React can be implemented with the help of the built-in function `React. lazy()`. This is also known as code splitting, In which `React.lazy` along with webpack bundler divides the code into separate chunks, when the component is requested the chunk is loaded on demand. The use of React Suspense is to define fallback content to be displayed during asynchronous components or data loading, as shown above. React Suspense provides better feedback to the user and improves the user experience as a user is not facing any blank screen or space while the content is being loaded. React Suspense is designed to handle the loading of the components that make asynchronous API requests. React Suspense can be used by wrapping the `` component and specifying the fallback content displayed while the component or data is loading....

Conclusion:

Lazy loading allows you to use server resources more efficiently by loading only the resources you need. This is very important for high-traffic applications or when server resources are tight.  A quicker initial load time can be achieved by using lazy loading, which minimizes the amount of code that must be downloaded and parsed when the page first loads. This can speed up your application’s first load time greatly....