In modern web development, optimizing the performance of our applications is crucial to provide a smooth user experience. An effective technique to achieve this is lazy loading, which involves loading only the necessary components and resources when they are actually required. In this blog, we will explore how to implement lazy loading in React using the function and the component, and we will see the benefits it brings to our . lazy Suspense applications Understanding Lazy Loading Lazy loading is a strategy that defers the loading of non-essential resources until they are needed, reducing the initial loading time and improving the overall performance of the application. In React, we can achieve lazy loading using the function and the component, introduced in React 16.6. lazy Suspense Using the Function lazy The function allows us to dynamically import a component, which is especially useful for code splitting and optimizing package size. lazy Here's how you can use it: const MyLazyComponent = React.lazy(() => import('./MyComponent')); In this example, will only be loaded when is rendered in the application. MyComponent MyLazyComponent Implementing Lazy Loading with the Component. The component is used to wrap components that are lazily loaded. It allows us to specify a fallback content that will be displayed while the lazy component is loading. Suspense Suspense Here's how it's used: import React, { lazy, Suspense } from 'react'; const MyLazyComponent = lazy(() => import('./MyComponent')); function App() { return ( <div> <h1>Lazy Loading Example</h1> <Suspense fallback={<div>Loading...</div>}> <MyLazyComponent /> </Suspense> </div> ); } In this example, while is being fetched, the fallback content ("Loading...") will be shown, providing a smooth transition for the user. MyLazyComponent Benefits of Lazy Loading By loading only the necessary components, the initial loading time of the application is reduced, enhancing the user experience. Faster Initial Loading: Lazy loading avoids unnecessary loading of resources, which can lead to faster navigation and interactions within the application. Performance Improvement: Lazy loading encourages code splitting, resulting in smaller package sizes that load more efficiently. Code Splitting: Users can interact with visible components more quickly, improving engagement and interaction.más rápidamente, mejorando la participación y la interacción. Increased User Engagement: Let’s Code a Real Example Imagine we are working on a web application containing several pages or routes, each represented by a different component. We want to optimize the application’s performance by ensuring that components are loaded only when the user needs them. https://codesandbox.io/s/priceless-pond-38qfks?from-embed=&embedable=true In the previous example, , , and are components created using and the function. This allows the components to be loaded lazily, meaning only when needed. HomePage AboutPage ContactPage lazy import() The function receives a function that returns a promise that loads the component. lazy() Within , routes are defined using the component. This component contains several elements that define the different routes of the application. Suspense Routes Route Each has a corresponding to the URL and representing the component to be rendered when the URL matches the path. Route path element If you want to learn more about you can check out my other article: React Router https://hackernoon.com/2-2-using-react-router-v6-for-private-and-public-routes-with-access-validation?embedable=true Conclusion Lazy loading is a powerful technique in React to enhance performance and user experience in our applications. By utilizing the function and the component, we can efficiently load components on demand, leading to faster loading times and more responsive applications. lazy Suspense Read more: Supercharge Your React Applications: 7 Best Practices and Techniques Protect Your Application by Hiding API Keys and Tokens in React - Beginners guide Want to connect with the Author? Love connecting with friends all around the world on . Twitter Also published . here