Optimizing React Loading Component

 

Component Load

In this blog, we’re discussing React loading components and the React lazy import feature. As you may or may not know, React is a Javascript framework that supports its architecture based on components and is used to build the front-end in our apps. Although what supports the entire app are the parts that make up these components and their relationships, in this tutorial, we will focus on the loading of those components when the application is running.

But, why is the loading of these components important? Well, the most significant reason is that the loading speed of the components can significantly affect the user experience of your application. Optimizing React loading components can improve loading speed, in turn, providing a better user experience.

For example, when you enter a web address and it takes more than 15 seconds to show something other than the white background, the usual reaction is to refresh the site. As developers, we know refreshing is the solution to a lot of common problems. However, in this case, the app just needed a few more seconds, and you did not give it the opportunity to finish loading, thus dismissing the product you had.

This is because, at the beginning of the development of your app, you have a small environment that can be tested quickly. As the product grows, optimizing React loading components (and in some cases using the React lazy import feature) becomes necessary as it becomes more complex to compile all that code into a small file.

Here is an example of how to optimize the loading of the components of an application. This app is used to manage a supermarket.

 
 
Code for an app used to manage a supermarket
 
 

When you think about how to bring the components into the app, the usual way is to import each component into your routes.

In the beginning, when compiling your app (without optimizing React loading components), you will realize there is no major problem with file sizes. However, as the app grows and grows, you have to import each main component that you create. This is the traditional way to load the components. But this method has an issue: importing the components this way forces the application to load all of them the moment it starts.

Utilizing the React Lazy Import Feature

When you use the router within the app, you will only see one component, depending on the URL. Then, why should you wait for all the components to load, if only one is on display at that time? The developers thought about this impediment with the React loading components and decided to give us a hand: ‘React lazy import.'

As its name implies, 'lazy import' does not load all components at once—only the immediately necessary ones—making the app load much faster.

In our example code, it looks like this:

 
 
Lazy Import code
 
 

With this strategy, you import the components in such a way that they only load when they are needed. Naturally, this is a much quicker process that provides the user with a streamlined experience.

However, a question arises: What would we show while loading that first component? The React loading component developers give us the option to add a previous block, called 'suspense.' This block allows us to render a view while loading the first component.

Our advice is to use it to inform the user that you are uploading the content. Any kind of 'loader' should do the job.

It's important to remember that you must use suspense when importing components with React lazy import, otherwise, you will have nothing to show while the app loads, and it will generate an error.

Now, our main code looks like this:

Main code with react lazy import

Now, the loader code:

Loader code

The developers created this React loading component solution as a simple way to help us improve the experience of our users from the moment they start loading the page. It is important to remember to start implementing it from the beginning of the product development, as it will also accelerate the compilation time of your app.

While this solution is very useful and can be considered a good React programming practice, it is not always necessary. Some components lose their value if all of their internal components have not been loaded correctly. In these cases, it is better to implement React loading components the traditional way, even if the load takes a little longer.

It is up to you to decide when it is worth making these changes! Be sure to browse our blog for more helpful development tips, and schedule a free pre-consultation meeting now if you’re ready to start a software project.


 
Alejandro ZakzukComment