- Published on
Suspense and Code-Splitting in React
- Authors

- Name
- Alamin Sheikh
- @alaminsheikh01
Code-Splitting in React
Code-Splitting is a technique for dividing your application's code into smaller chunks, which are loaded on demand instead of all at once. This technique helps to reduce the initial load time and provides a better user experience, especially for larger applications.
React supports Code-Splitting natively using dynamic imports and React.lazy(). With Code-Splitting, you can defer loading components that aren’t immediately needed until they’re required.
Example of Code-Splitting
Imagine you have a component HeavyComponent that is resource-intensive. Instead of loading it upfront, you can dynamically import it using React.lazy():
import React, { Suspense, lazy } from 'react';
// Use React.lazy to dynamically import the HeavyComponent
const HeavyComponent = lazy(() => import('./HeavyComponent'));
function App() {
return (
<div>
<h1>My App</h1>
{/* Suspense component to handle the lazy-loaded component */}
<Suspense fallback={<div>Loading...</div>}>
<HeavyComponent />
</Suspense>
</div>
);
}
export default App;
In this example:
HeavyComponentis only loaded when it's needed.- The
fallbackproperty in<Suspense>specifies what should be displayed whileHeavyComponentis loading (here, a"Loading..." message).
Suspense in React
Suspense is a component provided by React that can "suspend" rendering to wait for certain conditions, like loading a component or fetching data asynchronously. It works with React.lazy for component code-splitting, and in future versions of React, it will support data fetching as well.
Example of Suspense for Lazy Loading
Using the previous example, Suspense acts as a wrapper that displays a fallback (like a loading spinner or placeholder) while waiting for the dynamically loaded component:
import React, { Suspense, lazy } from 'react';
const HeavyComponent = lazy(() => import('./HeavyComponent'));
function App() {
return (
<div>
<h1>My App</h1>
{/* Fallback UI while HeavyComponent is being loaded */}
<Suspense fallback={<div>Loading...</div>}>
<HeavyComponent />
</Suspense>
</div>
);
}
export default App;
In this example:
Suspensewraps around theHeavyComponent, allowing it to "pause" rendering untilHeavyComponentis fully loaded.- During this pause, it displays the fallback UI.
Advantages of Suspense and Code-Splitting
- Improved Performance: Only the required code is loaded, resulting in faster initial load times.
- Better User Experience: Users see a loading indicator instead of a blank screen, which is particularly useful for slower network connections.
- Reduced Bandwidth: Only necessary components are loaded, making the app more efficient in terms of data usage.
Example of Code-Splitting Multiple Components
If you have multiple components that you want to load on demand, you can split each one using React.lazy.
const Header = lazy(() => import('./Header'));
const Footer = lazy(() => import('./Footer'));
const Sidebar = lazy(() => import('./Sidebar'));
function App() {
return (
<Suspense fallback={<div>Loading App Components...</div>}>
<Header />
<Sidebar />
<Footer />
</Suspense>
);
}
With this setup, each component (Header, Sidebar, Footer) is loaded only when it's about to be rendered. The Suspense component handles the loading state for all of them collectively.
Summary
- Code-Splitting reduces initial load times by breaking down your app into smaller chunks that load as needed.
- Suspense provides a fallback UI while components are loading, improving the user experience.
- Together, they make React applications faster and more efficient, especially for larger applications with multiple heavy components.
These techniques are essential for building scalable React applications that perform efficiently across different network conditions and devices.