Is your React program too slow or taking too long to load?

If so, you might want to use a technique known as code splitting.

This technique is very effective at improving the load speed and performance of React applications.

Man using both a laptop and a phone

But what is code splitting?

And how is it done?

What Is Code Splitting?

A typical React tool comprises dozens of components (and code).

But you don’t need to load most of these components when you load them for the first time.

Code splitting entails splitting out the different parts of your tool and only loading them when needed.

This is far more efficient than loading the entire app at once.

Because you’re not actually on those pages yet.

The idea of code splitting is to see to it you load the code only when it’s needed.

Next, go to the Source tab.

There you’ll find all the code that’s downloaded as you navigate to the page.

Without code splitting, the surfing app downloads all the files in your project on the initial page load.

This can slow down your website if it contains a lot of files.

Code splitting becomes especially useful as your project starts to get larger and larger.

This is because downloading the entire program files at once can take a very long time.

So splitting that up is going to be quite beneficial.

Ourintroductory guide on ReactJSexplains components and functions in-depth in case you need a refresher.

Code Splitting Functions: Using Dynamic Import

Consider the following situation.

You want your homepage to have a button.

So you create aHome.jscomponent and define the view of your homepage.

In this case, you have two options.

First, you could import the code for adding the numbers at the top of theHome.jsfile.

But here’s the problem.

A better approach will be to load thesum()function only when you tap the button.

To achieve this, you’ll need to perform a dynamic import.

This means that you’ll import thesum()function inline in the button element.

This improves the loading time of the homepage.

The best place to perform code splitting would be inside your router.

Because this is where you map components to routes in your utility.

Let’s suppose that your app has aHome,About, andProductscomponent.

When you’re at theHomecomponent, there’s no point in loading theAboutcomponent or theProductscomponent.

So you gotta split them away from theHomeroute.

For this reason, theSuspensecomponent has afallbackproperty.

In our case, the value is simple text that says “Loading…”.

So while each of the pages is being downloaded, it’s going to say loading on the screen.

This is the same for the Products page.

This could be an admin dashboard that shows up for admin users, but not for normal users.

In this case, you wouldn’t want to show all of that data every single time.

Conditional code splitting uses the same concept asconditional rendering in React.

But there are several other methods as well that can give you the required knowledge to create robust applications.