Lazy Loading is a technique in React that allows you to load components only when they are needed. This helps improve the performance of your application by reducing the initial load time, as only the required parts of the app are loaded at first, and the rest is loaded dynamically when necessary.
React provides the React.lazy function and the Suspense component to implement lazy loading.
const LazyComponent = React.lazy(() => import('./path/to/Component')); function App() { return ( <React.Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </React.Suspense> ); }
import React from "react"; import HeavyComponent from "./HeavyComponent"; function App() { return ( <div> <h1>App Component</h1> <HeavyComponent /> </div> ); } export default App;
In this example, the HeavyComponent is always loaded, even if it’s not immediately needed, increasing the initial load time.
import React, { Suspense } from "react"; const HeavyComponent = React.lazy(() => import("./HeavyComponent")); function App() { return ( <div> <h1>App Component</h1> <Suspense fallback={<div>Loading Heavy Component...</div>}> <HeavyComponent /> </Suspense> </div> ); } export default App;
Now, HeavyComponent is loaded only when it is rendered. The fallback UI (e.g., "Loading Heavy Component...") is displayed while the component is being fetched.
Lazy loading is especially useful in routing to load components for specific routes only when those routes are accessed.
import React, { Suspense } from "react"; import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; const Home = React.lazy(() => import("./Home")); const About = React.lazy(() => import("./About")); const Contact = React.lazy(() => import("./Contact")); function App() { return ( <Router> <Suspense fallback={<div>Loading Page...</div>}> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="/contact" element={<Contact />} /> </Routes> </Suspense> </Router> ); } export default App;
You can lazy-load multiple components and combine them with conditional rendering.
const LazyComponent = React.lazy(() => import('./path/to/Component')); function App() { return ( <React.Suspense fallback={<div>Loading...</div>}> <LazyComponent /> </React.Suspense> ); }
If the lazy-loaded component fails to load (e.g., network error), React does not provide built-in error handling for lazy loading. You can use ErrorBoundary to handle such scenarios.
import React from "react"; import HeavyComponent from "./HeavyComponent"; function App() { return ( <div> <h1>App Component</h1> <HeavyComponent /> </div> ); } export default App;
Lazy loading in React is a powerful way to improve application performance and optimize the user experience. By loading components dynamically using React.lazy and Suspense, you can reduce the initial load time and ensure that only the necessary parts of your application are fetched.
The above is the detailed content of Improve React App Performance with Lazy Loading Components. For more information, please follow other related articles on the PHP Chinese website!