Code spliting in React via lazy and suspense

Load Application faster with code spliting and dynamic loading
First published: 2021-05-13      |      Last updated: 2025-06-25

Everyone wants their application so fast, but how to achieve that, in that case, code splitting comes into the picture, where we split code and set the priority loading of the code snippet.

But how do we set the priority or load them whenever required?

1. React.lazy()

Using this we can set code priority to the lower or it will load only whenever required. So how to do that? it's easy, let's understand through an example.

1//generally approach to import Component
2 import Blog from './BlogComponent';
1// This component is loaded dynamically or lazy load
2const BlogComponent = React.lazy(() => import('./BlogComponent'));

2. React.Suspense()

Once the component is set for a lazy load then we need to set some kind of fallback option as well, till that code is rendered. This fallback option either could be a loader icon, screen, image, etc.

1// This component is loaded dynamically or lazy load
2const BlogComponent = React.lazy(() => import('./BlogComponent'));
3function AppComponent() {
4return (
5// Displays <LoaderSpinner> until BlogComponent loads
6<React.Suspense fallback={<LoaderSpinner />}>
7<div>
8<BlogComponent />
9</div>
10</React.Suspense>
11);
12}

Abhimanyu Singh Rathore
By Abhimanyu Singh RathoreHe is a fun-loving technocrat, artist, photographer, nature lover, leisure traveler, and developer. He actively develops full-stack apps and programs in Go and various JavaScript frameworks and libraries, especially React.
cardImage

The State of Consumer Digital ID 2024

cardImage

Top CIAM Platform 2024

cardImage

Learn How to Master Digital Trust

Customer Identity, Simplified.

No Complexity. No Limits.
Thousands of businesses trust LoginRadius for reliable customer identity. Easy to integrate, effortless to scale.

See how simple identity management can be. Start today!