In this example, when LazyComponent is loading, users will see the text "Loading…". It’s a simple and effective way to keep them informed and avoid a blank screen.
First, if you haven’t got one, create a React app:
npx create-react-app lazy-loading-demo cd lazy-loading-demo
If you’re already working on an existing project, just follow along.
Let’s create a new component, for example, LazyComponent.js:
import React from 'react'; const LazyComponent = () => { returnThis is a lazy-loaded component!; }; export default LazyComponent;
Import lazy and Suspense in the file where you want to use the component, typically App.js:
import React, { Suspense, lazy } from 'react'; // Lazy load the component const LazyComponent = lazy(() => import('./LazyComponent'));
To handle the loading state, wrap the lazy-loaded component with Suspense:
function App() { return (}>Hello, World!
Loading...
Start the server:
npm start
You should see the "Hello, World!" message instantly. When you interact with LazyComponent, the fallback message "Loading..." will appear while it’s being fetched.
React Lazy Loading can also be applied to route-based components, making sure different routes only load as needed. Here’s how to do it using React Router:
npm install react-router-dom
import React, { Suspense, lazy } from 'react'; import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom'; const LazyComponentA = lazy(() => import('./components/ComponentA')); const LazyComponentB = lazy(() => import('./components/ComponentB')); function App() { return (Loading...
With route-based lazy loading, only the components for the current route will load, making navigation faster and smoother.
React Lazy Loading is common across all kinds of platforms, from social media to eCommerce.
Explore Further: Angular to React Migration
React Lazy Loading is an effective way to speed up your React app by only loading what’s needed at the time. With React.lazy and React.Suspense, you can easily implement this technique and see improvements in load times and user experience. Faster apps lead to more engaged users, fewer bounce rates, and an overall better impression.
If you’re building a larger app, React Lazy Loading can really help keep things manageable and efficient. Plus, it’s easy to set up with just a few lines of code.
Interested in more ways to improve your app’s performance? We at Prioxis know that performance matters. There are a lot of simple tweaks, like lazy loading, that can make a big difference in how your app performs. And we’re here to help if you need a hand. Feel free to reach out!
Use React Lazy Loading for large components, heavy media files, or routes that aren’t needed right away. It’s great for secondary features or pages users might not visit often.
If your app uses client-side rendering, React Lazy Loading could affect SEO since crawlers may not load JavaScript properly. But if you use server-side rendering (SSR), React Lazy Loading won’t interfere with SEO.
Yes! React.lazy works seamlessly with React Router. It ensures that components specific to a route only load when that route is accessed, improving initial load performance.