Implementing Lazy Loading for Functional Components in React
Last Updated :
26 Mar, 2024
React is a popular library, maintained by Meta, which is used for building user interfaces and dynamic websites. It offers several techniques to optimize performance and enhance end-user experience. Lazy loading is one such technique that can be used to improve the performance of web applications by deferring the loading of resources until the browser needs them.
This article will dive deep into lazy loading and how to implement it in a React application.
Prerequisites:
Approach:
React.lazy() enables lazy loading of components, loading them only when needed, not with the initial bundle. Suspense, a built-in React component, manages the loading state, displaying a fallback UI during component fetch. Upon completion, Suspense replaces the fallback UI with the actual component, enhancing React app performance by asynchronously loading components.
Steps to Implement Lazy Loading in React:
Step 1: Set Up a New React App
Open your terminal and run the following commands to create a new React app:
npx create-react-app lazy-loading
cd lazy-loading
Step 2: Create Components
After setting up react environment on your system, we can start implementing components for lazy loading.
Folder Structure:
Project Structure for lazy loading projectThe updated dependencies in package.json file will look like this:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
Example: `ParentLazyComponent` uses React.lazy() to lazily load `DynamicComponent` when it's needed. The Suspense component is used to display a loading indicator while `DynamicComponent` is being loaded. This kind of approach simplifies makes sure that components like `DynamicComponent` are not loaded in initial application bundle and loaded only when needed in case of complex UI applications.
JavaScript
import ParentComponent from './components/ParentComponent';
function App() {
return (
<div className="App">
<ParentComponent />
</div>
);
}
export default App;
JavaScript
import React from 'react';
const DynamicComponent = () => {
return (
<div>
<h1>GeeksForGeeks</h1>
</div>
);
};
export default DynamicComponent;
JavaScript
//ParentComponent.js
import React, { Suspense } from 'react';
const DynamicComponent =
React.lazy(() => import('./DynamicComponent'));
const ParentLazyComponent = () => (
<Suspense fallback={<div>Loading...</div>}>
<DynamicComponent />
</Suspense>
);
export default ParentLazyComponent;
Step 3: In your terminal, make sure you're in the project directory and run below command:
npm start
Output:
Lazy Loading Output
Similar Reads
Lazy Loading in React and How to Implement it ? Lazy Loading in React is used to initially load and render limited data on the webpage. It 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 limi
4 min read
Why do you need to import React in functional components ? React is a declarative, efficient, and flexible JavaScript library for building user interfaces. Itâs âVâ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. In react, we use JSX, especially in the functional important which is
3 min read
How to implement useContext Hook in a Functional Component ? In React, the useContext hook is used to consume values from a context. Context in React is a way to share values like themes, authentication status, or any other global state between components without explicitly passing the props through each level of the component tree. Syntax of useContext Hook:
2 min read
How to create Functional Components in React ? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
Migrating from Class Components to Functional Components in React React version 16.8.0 or higher allows users to write the functional components without the use of class components. Using class components, we need to write the whole class to interact with the state and lifecycle of the component. The code was quite complex and harder to understand which led to low
4 min read